Error in email authentication regular expression
I am trying to validate my client side email address field using jQuery and regex, but for some reason javaScript is complaining about a syntax error in regex as
@((?:[\w-]+\.)*\w[\w-]{0,66})
of
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
JQuery function
$('.emailField').change(function () {
var inputVal = $(this).val();
var emailReg = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (!emailReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-7">Invalid Email Format.</span>');
}
});
+3
source to share
2 answers
I have a separate plugin for this and it gets called on my partial view and it worked!
(function ($) {
$.fn.validateEmail = function (_email) {
alert(_email);
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(_email);
};
}(jQuery));
+1
source to share
try below code using JavaScript RegExp property. read more here
var emailReg = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
if (!emailReg.test(inputVal)) {
$(this).after('<span class="error error-keyup-7">Invalid Email Format.</span>');
}
+1
source to share