Email address validation at client and server side
Email validation at client side(Java Script)
---------------------------------------------
function validateEmailAddress(emailAddress) {
var expression = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return expression.test(email);
}
This function will return true if it is a valid email address otherwise false if it is invalid email address.
----------------------------------------------
Email validation at server side using Java
-----------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailAddressValidator{
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_ADDRESS_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public EmailAddressValidator(){
pattern = Pattern.compile(EMAIL_ADDRESS_PATTERN);
}
public boolean validate(final String emailId){
matcher = pattern.matcher(emailId);
return matcher.matches();
}
public static void main(String [] arg){
String emailId="abc@yahoo.com";
EmailAddressValidator validatorObj=new EmailAddressValidator();
System.out.println("-validate email address:"+emailId+": ---is it proper email:"+validatorObj.validate(emailId));
emailId="abc @";
System.out.println("-validate email address:"+emailId+": ---is it proper email:"+validatorObj.validate(emailId));
}
}
--------------------------------------------
At server side you can use this class as a utility class and use wherever you want.
Comments
Post a Comment