I have set up email validation for a Select2 list with help of a regular expression.
It should only accept emails that have a pattern like [email protected] (alt [email protected]). The validation works for the domains but the validation for fn+ln doesn’t work, it accepts entries like [email protected] which it shouldn’t.
What am I doing wrong?
Here is my set up:
$(document).ready(function() {
var noResults = '#{javascript: return utilityBean.escapeJS(xptI18NBean.getValue("strings.gen_enter_email"))}';
x$("#{id:select2Example}").select2({
dropdownAutoWidth: true,
width: 'resolve',
allowClear: true,
createTag: function(term, data) {
var value = term.term;
if (validateEmail(value)) {
return {
id: value,
text: value
};
} else {
//not valide yet?
}
return null;
},
"language": {
"noResults": function() {
return noResults;
}
}
});
});
function validateEmail(email) {
var regex_email = /^(([w-]+(?:.[w-]+)*)@(acme.org|acme.com))$/;
return regex_email.test(email);
}
1