I have a string format like "firstname lastname(email)" and I want to validate the entered value according to this format using jQuery. Hows it is possible? I have tried many things but those are not working properly.
try this regular expression
/^([a-zA-Z]+ )+([a-zA-Z])+([\(])+([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+(\))+$/
$.validator.addMethod("username_email",function(value,element){
return this.optional(element) || /^([a-zA-Z]+ )+([a-zA-Z])+([\(])+([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+(\))+$/i.test(value);
},"enter firstname lastname(email)");
There's no way to accurately and concisely validate an email. An absolutely correct regex would be very long (and most email servers don't support all formats of email address that the RFC defines anyways). You can assume the email is a sequence of valid characters, then an at-sign, then a sequence of valid characters. If the regex for email is /[\w.+-]+\@[\w.]+/
, then the full regex (if you want to forbid a middle name) is
/[a-zA-Z]+ [a-zA-Z]+\([\w.+-]+\@[\w.]+\)/