0

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?

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Searching
  • 98
  • 1
  • 8

4 Answers4

3

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.

Community
  • 1
  • 1
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Note that MX record is not necessary to make the domain deliverable. In the absence of an MX record, MTAs ought to fall back to resolving the A or AAAA record itself. so a reasonable approach to the answer might be "if the domain has an MX record, a A record, an AAAA record (or via CNAME it resolves to one, it's valid" – djsadinoff Feb 03 '22 at 09:08
2

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

Er.Shreyansh Shah
  • 1,482
  • 1
  • 9
  • 29
  • This does not answer the question, and even duplicates previously posted answer. Please, revise your answer. – Fahri Azimov Feb 03 '16 at 10:18
  • @FahriAzimov Whilst you may disagree with it you shouldn't be telling them to remove it. If you disagree with their answer then just downvote and leave feedback but don't be telling them to remove it. Also it's not a duplicate of the other answer this is different code and provides detail to their answer so bad comment. Read through some of your other comments to other answers I'm not sure you're fully aware of some of the rules yourself. – Popeye Feb 03 '16 at 10:19
1

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

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

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!

Community
  • 1
  • 1
Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29