I'm using a regex below to validate email to accept alphanumeric characters. It works in the following cases
1) Must contain atleast one alphabets 2) allow alphanumeric 3) allow special characters .-and _
Regular Expression:
/^([a-zA-Z0-9])(([a-zA-Z0-9])*([\\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\\-])+(\\.))+([a-zA-Z]{2,4})+$/i
Cases:
1111@gmail.com - allow
aaaa@gmail.com - allow
aa1_aa@gmail.com - allow
Output expected:
1111@gmail.com - not allow because it does not contain alphabets before @
aaaa@gmail.com - allow
a1@gmail.com - allow
1a@gmail.com - allow
aa1_aa@gmail.com - allow
Hers is jsfiddle Demo
Your regex will do the job, just add this at the beginning
(?=[^@]*[A-Za-z])
making your final regex like this:
/^(?=[^@]*[A-Za-z])([a-zA-Z0-9])(([a-zA-Z0-9])*([\\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\\-])+(\\.))+([a-zA-Z]{2,4})+$/i
(?=exp)
is positive look-ahead. It will try to find the expression without taking it into match. look-ahead actually matches characters, but then gives up the match.
(?=[^@]*[A-Za-z])
: will match [^@]*[A-Za-z]
, meaning anything other than @
followed by a alphabet. So actually it will match if at least one alphabet is present in the part before @
Here is the JavaScript code:
var email_to_check = "1111@gmail.com";
email_check=email_to_check.substring(0,email_to_check.lastIndexOf("@"));
var isnum = /^\d+$/.test(email_check);
var email_regex = /^([a-zA-Z0-9!@#$%^&*(){}|:"<>?\/';\\+\-~]*@[a-zA-z]+\.[a-zA-z]+)$/;
email_test = email_regex.test(email_to_check);
if(isnum){
alert("You must enter aleast an Alphabet !")
}else{
if(email_test){
/* code if email is right :) */
alert("This is corrrect email !")
}else{
alert("Enter valid email address !");
}
}
Remove the special char which you don't want in your checklist.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.