Reputation: 27
I'm trying to use a regular expression validation using .match for email validation and for some reason it only shows invalid even though all formats are followed!
javascript function
function check_email_valid(emails) {
var emailRegex = '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';
if (emails.match(emailRegex)) {
jQuery('#<%=Label16.ClientID%>').css('color', 'green');
jQuery('#<%=Label16.ClientID%>').show();
jQuery('#<%=Label16.ClientID%>').text("Valid Email!");
}
else {
jQuery('#<%=Label16.ClientID%>').css('color', 'red');
jQuery('#<%=Label16.ClientID%>').show();
jQuery('#<%=Label16.ClientID%>').text("Invalid Email!");
}
Event trigger
$('#<%=TextBox8.ClientID%>').keyup(function () {
var email = jQuery('#<%=TextBox8.ClientID%>').val();
check_email_valid(email);
});
I keyed in [email protected] and got "Invalid Email!". Any idea why?
Upvotes: 0
Views: 280
Reputation: 70490
Case-sensitivity.... And validating email addresses is a bit more complicated then that... (.museum for one, but there is more..).
Upvotes: 1
Reputation: 18682
JavaScript has a slightly different way of defining regex patterns than other languages. They're not strings, but instead simply start with a slash, end with one, and after that you can specify some flags.
Your pattern is defined like this:
var emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
The i flag at the end of the pattern makes it case insensitive.
Upvotes: 0