Reputation: 108
I am developing an iPhone application where I need the user to give his email address at login.
What is the best way to check if an email address is a domain name valid or not?
Upvotes: 0
Views: 7511
Reputation: 11770
As @trojanfoe suggested, using MX record lookup you can check whether entered domain is a mail server. Here is an objective c version of MX record lookup, you need to initialize DNSServiceRef
with kDNSServiceType_MX
service type.
Another, the most reliable option to check whether user provided valid e-mail address or not would be sending e-mail with confirmation code to the entered e-mail address.
Good luck!
Upvotes: 0
Reputation: 31627
Below is what I use for email validation.
NSString *emailRegex = @"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if (![emailTest evaluateWithObject:emailTF.text]) {
// wrong email
} else {
// right email...
}
If you want to check for domain, go with below.
NSPredicate *websitePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[A-Za-z0-9]+(.[A-Za-z0-9-:;\?#_]+)+"];
if ([websitePredicate evaluateWithObject:@"google.com"]) {
NSLog(@"valid domain");
} else {
NSLog(@"not valid domain");
}
I hope you are looking for this...
If you are looking for actual validation of domain name (& not format of domain), then you should follow @trojanfoe answer
Upvotes: 1
Reputation: 1482
To check email address :-
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if( [emailTest evaluateWithObject:email]){
//Valid email
}else{
//Wrong Email id
}
We can check domain name but we can't say that this is valid or not because domain name is not fixed ex:- 1)[email protected]
We can check specific domain name as email address contains "gmail.com" or "yahoo.com"
It's not fix because domain name format is not fix.
It might be like :-
Upvotes: 2
Reputation: 122381
If it's really important to you then you could attempt to look-up the MX record of the domain specified, via DNS.
See this answer for (Linux) C code to do that.
Upvotes: 3