Domain Name email Validation - ios

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

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!

Related

Validate string with various validation in iOS

I've been facing some issue with to valid string with following condition. The respective conditions are as follows:
String should contain MAX length(which is 7) and should not less Than 6
First character must be from A-Z (should consider aUppercase only)
remaining character must contain only digit (0 to 9).
Here is an example of String I want to valid A12342 (desire output with validation)
Thanks in advance ,Any help will be appreciated.If any one need more information about my query please let me know .
-(BOOL)CheckConditionForValidation
{ if([textfield.text isequalToString:#""]){
return FALSE
}
//else if (//validation for my specific number)
//{
//want to implement logic here
//}
}
Try this rejex pattern [A-Z][0-9]{5,6}
check it online with the link Online rejex check
and if it work than use like this
- (BOOL)checkValidation:(UITextField *)textField
{
NSString *rejex = #"<your pattern>";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", rejex];
//if rejex fullfil than it will return true else false.
return [emailTest evaluateWithObject:textField.text];
}

NSPredicate ANY with multiple columns

I've 2 tables Person and Social.
Person has multiple Socials.
Socials has username and Service.
I'm trying to write a predicate on Person table which will match the people with proper username and service from the Social table. The statement below is wrong. Please let me know if there is a proper syntax to achive the same or if there is any better method to achieve the same.
[predicates addObject:[NSPredicate predicateWithFormat:#" ANY (socials.username = %#
AND socials.service = %#)",#"username",#"service"]];
That is a typical use-case for a SUBQUERY:
[NSPredicate predicateWithFormat:#"SUBQUERY(socials, $s, $s.username = %# AND $s.service = %#).#count > 0",
#"username", #"service"]

Core data predicate on one-to-many relationship with contains

In my Core Data model, I have Conversation class, that has a 'participants' relationship, elements of which are instances of Participant class. Participant class has an href field that is a string.
Using MagicalRecord, I'm trying to fetch all Conversation instances that have at least one participant whose href contains '/businesses/%#', where %# is a given identifier. In business terms, that means loading all the conversations in which a business with a given identifier is a participant.
Here is the code that is supposed to do the fetching:
NSPredicate *participationFilter = [NSPredicate predicateWithFormat:#"ANY participants.href contains[cd] '/businesses/%#'" argumentArray:#[identifier]];
NSArray *conversations = [Conversation MR_findAllWithPredicate:participationFilter];
But conversations is always empty, even though I know that such conversations are in my database. What am I doing wrong?
'...' in a predicate is taken as a literal string, and the %# placeholder inside the
quotation marks is not expanded. You have to build the combined string first:
NSString *searchTerm = [NSString stringWithFormat:#"/businesses/%#", identifier];
NSPredicate *participationFilter = [NSPredicate predicateWithFormat:#"ANY participants.href contains[cd] %#", searchTerm];

Postal Code Validation in iOS

I am developing an app for iOS 7 in which I want to retrieve user location details.
Now I also want postal code of user. And I want to check on only that page that postal code entered by the user is valid or not.
I want to validate for US and UK.
How to achieve this?
For India, you can use the below method to validate the pincode
-(BOOL)isValidPinCode:(NSString*)pincode {
NSString *pinRegex = #"^[0-9]{6}$";
NSPredicate *pinTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", pinRegex];
BOOL pinValidates = [pinTest evaluateWithObject:pincode];
return pinValidates;
}
For US, you can use
^\d{5}(-\d{4})?$
For UK, use this
^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$
Swift 3.0
For US, you can use the below method to validate the ZipCode
func validZipCode(postalCode:String)->Bool{
let postalcodeRegex = "^[0-9]{5}(-[0-9]{4})?$"
let pinPredicate = NSPredicate(format: "SELF MATCHES %#", postalcodeRegex)
let bool = pinPredicate.evaluate(with: postalCode) as Bool
return bool
}
You can use this answer to get postal code.
Get the Zip Code of the Current Location - iPhone SDK
As for validation, it will depend on the country the postal code is in, but this may be a good starting point.
how to validate Zipcode for US or Canada in iOS?

NSPredicate predicateWithFormat passing in name of attribute

Simple question regarding NSPredicate's. I'm trying to construct my predicate with "passed in" values like so :
NSPredicate* currentPredicate = [NSPredicate predicateWithFormat:#"%# == %#",key,[changesDict valueForKey:#"Id"] ];
However, I haven't been able to get this to work correctly. If I insert the actual value I pass through it does work though. So this works :
NSPredicate* currentPredicate = [NSPredicate predicateWithFormat:#"contactId == %#",[changesDict valueForKey:#"Id"] ];
(Notice i inserted contactId as opposed to the previous example where I pass a string by the same name)
To troubleshoot I NSLogged the two predicates looking at their descriptions and they were different. I'll show them below.
This is the working one
2013-01-17 10:29:25.513 TestingCoreData[1776:3b03] contactId == "5878"
This is the non working one
2013-01-17 10:29:25.513 TestingCoreData[1776:3b03] "contactId" == "5878"
So I can kind of see that it's inserting a string where I really just want the name of the attribute that i'll later be using in the fetch request. But is there any way to accomplish this by passing in values?
For keys or key paths, you have to use the %K format:
[NSPredicate predicateWithFormat:#"%K == %#", key, [changesDict valueForKey:#"Id"]];
(See Parser Basics in the "Predicate Programming Guide".)

Resources