0
Я один input
Select2 Тэги: Добавить теги формат электронной почты на входе после нажатия клавиши "Enter", "Космос", "Tab"
<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>
в тег не добавлен, какое-то предложение?
Вы не должны помещать регулярное выражение внутри цитаты? –
@ISuthanBala метод validateEmail работает нормально, в консоли я тестировал, и он работает нормально – Alex