0
我有一個input
選擇二標籤:輸入添加電子郵件格式的標籤後,按鍵「輸入」,「空間」,「標籤」
<label id="email2_error">Email is not in the correct format</label>
<input type="text" style="width:500px" id="emailTags" placeholder="add email" />
按鍵之後我打電話的方法addTag
<script type="text/javascript">
$(document).ready(function() {
$("select#emailTags").select2({
placeholder: "[email protected]",
allowClear: true
});
jQuery("#emailTags").keypress(function (event) {
//See if the key pressed is 'space'
if (event.which == 32) {
addTag();
}
//See if the key pressed is 'enter'
if (event.which == 13) {
addTag();
}
//See if the key pressed is 'tab'
if (event.which == 8) {
addTag();
}
});
});
function addTag() {
var email = $('#emailTags').val()
if (!validateEmail(email)) {
$("label#email2_error").show();
return false;
} else {
$("label#email2_error").hide();
//Adding the tag
$('#emailTags').select2('val', $('#emailTags').val());
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
</script>
的標籤沒有添加,有人建議?
你不是應該把報價裏面的正則表達式? – 2014-10-06 16:31:03
@ISuthanBala方法validateEmail做工精細,在控制檯我測試,它工作正常 – Alex 2014-10-06 16:39:57