I was reading this link to understand regex: https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html
In my code, Here is what I have:
var reg = /^([A-Za-z0-9_\-\.]) \@([A-Za-z0-9_\-\.]) \.([A-Za-z]{2,4})$/;
I was testing this email: "[email protected]", and I got error "Please Enter Valid Email ID"
If I understand correct, it is related to the ending section .([A-Za-z]{2,4})$/
I though the {2,4} means 2 to 4 characters, so I changed to {2,6}, but still getting error. Basically the ".works" can be any number of characters.
Can I know what I do wrong?
Kind Regards
CodePudding user response:
Parsing emails for validity using a regex is a non trivial problem.
These are all valid emails that your regex will fail
'Long live the king'@com
This\ is\ my#@email.address
Fred\@[email protected]
See https://beesbuzz.biz/code/439-Falsehoods-programmers-believe-about-email and look up the bits about email addresses.
CodePudding user response:
I'd just use filter_var
with the FILTER_VALIDATE_EMAIL
flag to check, if an email address is in the correct format.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email address
}