Domain Name email Validation

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?


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.


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)abc@abc.com

2) abc@gmail.com

3) abc@yahoo.com

4) abc@abc.in

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 :-

1) aaa@aaa-a.com

2) aaa@aaa.co.in

3) aaa@hotmail.com

4) aaa@facebook.com


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...
}

Edit 1

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...


Edit 2

If you are looking for actual validation of domain name (& not format of domain), then you should follow @trojanfoe answer

链接地址: http://www.djcxy.com/p/42192.html

上一篇: JavaScript方法导航到其他网址

下一篇: 域名电子邮件验证