I want to validate an email validation of type 'abc@xyz.com.com' Should show an error message if they enter invalid domain name. Would be very grateful for your help.
-
1There's nothing invalid about that; "xyz.com.com" is a perfectly valid domain. – Ernest Friedman-Hill Mar 29 '12 at 12:11
-
3see answer on this: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Mar 29 '12 at 12:12
4 Answers
I find that trying to validate an e-mail adress thoroughly is too much work and I tend to use something along the lines of /\A[^@]+@[^@]+\z/. Either way you will still have to send a confirmation e-mail in order to verify that the adress is in use.
Also see this page about writing a regex for an e-mail address.
Additionally, I think in a lot of cases where a user mistypes an e-mail address they still type an address in valid format (on top of that, a malicious user can always write e.g. example@example.com and get away with it)

- 7,316
- 3
- 20
- 23
I think you firstly need to validate email by regexp then query DNS to see if host part of the email address is valid. Here is example code that makes that cheks.
/**
* Checks validity of given email addess
* Also checks domain name for validity via DNS
*
* @param string $email
* @return boolean
*/
function checkEmailValididty($email){
$isValid = false;
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
$host = substr($email, strpos($email, '@') + 1);
if(function_exists('getmxrr') and getmxrr($host, $mxhosts) != false){
$isValid = true;
}
elseif(gethostbyname($host) != $host){
$isValid = true;
}
}
return $isValid;
}

- 1,374
- 1
- 18
- 30
-
1Though this seems cool, the docs for getmxrr say "Note: This function should not be used for the purposes of address verification. Only the mailexchangers found in DNS are returned, however, according to » RFC 2821 when no mail exchangers are listed, hostname itself should be used as the only mail exchanger with a priority of 0.". Does anyone have any thoughts on this - I've not used getmxrr but it looks potentially very useful. – Rich Bradshaw Mar 30 '12 at 07:38
It is very easy to check username till @ operator but, i think, we can not check domain name is valid or not. because, you knew thousands of domain today. tomorrow, any organization or anything else will add more domains then how can we check for that.
The only thing is that, we can make restriction on length.
if i am wrong please correct me.. would be grateful for help..thank you.
-
1 thing that i forgot to include is that, we can check emails by using regular expressions. I am also searching for domain names...i did not find any solution to above problem. if anyone know how to solve then please help... – Mar 29 '12 at 12:29
-
var reg = /^[A-Z0-9._]+@[A-Z0-9.]+[A-Z]{2,3}/i; is my regular expression for checking emails...till now, i m checking for domain names...hoping for +ve reply...thanks – Mar 29 '12 at 12:31
Ok, i am not sure why most here skips this, but there's a function called filter_var which can receive a parameter to verify if the string is a valid email. It does not check if the domain exists or something else. Also here you can find the available filters you can use for validation.
If you need to filter domains, there is a easy way to extract the domain from the email and disallow those that you don't want.
You can do a easy check if the requested domain exists, if that is what you like.

- 502
- 2
- 9