Friday, February 19, 2021

email validation with regular expressions using jquery

For email address validation there is Official Standard: RFC 5322 but the regex to match this standard is very complex and would not be suitable for implementation in our applications, but the below regex is the most appropriate to be used in our applications. I didn’t find a bug till now.

Regex:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:    
[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"

Sample Javascript to validate the format of email address:

var regex = [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?;
if(regex.test(email)){
//valid email
}

No comments:

Post a Comment