Email validation PHP Script
Email validation PHP Script
The below function will valide the email address,
Function to validate email address in PHP
function validateEmail($email) { if(eregi('^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email)) return true; else return false; }
Let’s see the explanation of the following regular expression to validate email address in php
^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$
The “^” sign represents the start of the string.And, [a-zA-Z0-9._-]+ represents the first part of string before “@” sign can consists alpha bates, digits and “.“,“-” and “_” signs. After that “@” refers that this sign must exist. The next part is name of the domain and “[a-zA-Z0-9-]+” allow alpha bates, digits and “-” sign. After that period(.) should exist and validated by “\.“. And, the next string is TLD or ccTLd so can contain only two to four alphabates and validated by “[a-zA-Z]{2,4}“. The next part of expression “(\.[a-zA-Z]{2,3})?” refers that there will be another two or three alphabates after period(.) but this part is optional which is represented by “?” sign. And the last part is same as previous part and is optional as well.