Checking if a string is a number on iPhone - iphone

How can I check if an input string is a number like x.y?

Try this code,
NSString *nameRegex =#"[0-9]+\\.[0-9]$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", nameRegex];
BOOL isDecimalNumber=[nameTest evaluateWithObject:string];

There is one more solution.
NSString *str = #"123456";
NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
BOOL valid = [[str stringByTrimmingCharactersInSet: decimalSet] isEqualToString:#""];

Related

NSPredicate search for number in NSString

I have to find number in NSString using NSPredicate. I am using following code.
NSString *test = #"[0-9]";
NSString *testString = #"ab9";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(%# CONTAINS[c] %#)", test,testString];
BOOL bResult = [predicate evaluateWithObject:testString];
This code is searching for number at only start. I have also tried with #"[0-9]+" and #"[0-9]*" theses expressions but not getting correct result.
Use this
NSCharacterSet *set= [NSCharacterSet alphanumericCharacterSet];
if ([string rangeOfCharacterFromSet:[set invertedSet]].location == NSNotFound) {
// contains A-Z,a-z, 0-9
} else {
// invalid
}
See if it works
When you say
[predicate testString]
You're actually sending 'testString' message (ie: calling 'testString' method) into predicate object. There is no such thing.
I believe what you should be sending instead is 'evaluateWithObject' message, ie:
BOOL bResult = [predicate evaluateWithObject:testString];
The evaluateWithObject method reference says:
Returns a Boolean value that indicates whether a given object matches
the conditions specified by the receiver.
Use NSCharacterSet to analyse NSString.
NSCharacterSet *set= [NSCharacterSet alphanumericCharacterSet];
NSString testString = #"This#9";
BOOL bResult = [testString rangeOfCharacterFromSet:[set invertedSet]].location != NSNotFound;
if(bResult)
NSLog(#"symbol found");
set = [NSCharacterSet uppercaseLetterCharacterSet];
bResult = [password rangeOfCharacterFromSet:set].location != NSNotFound;
if(bResult)
NSLog(#"upper case latter found");
set = [NSCharacterSet lowercaseLetterCharacterSet];
bResult = [password rangeOfCharacterFromSet:set].location != NSNotFound;
if(bResult)
NSLog(#"lower case latter found");
set = [NSCharacterSet decimalDigitCharacterSet];
bResult = [password rangeOfCharacterFromSet:set].location != NSNotFound;
if(bResult)
NSLog(#"digit found");

Alter reg-ex for email Validation on iphone

I am trying to validate email using reg-ex. here is the code...
+ (BOOL) stringIsValidEmail:(NSString *)checkString;
{
NSString *emailRegEx =
#"(?:[a-zA-Z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
#"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
#"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-"
#"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
#"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
#"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
#"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
return [emailTest evaluateWithObject:checkString];
}
now I don't have much knowledge about regex but this accepts a#a.c as a valid email. But this should not be the case and at least two characters should be required at the end. What paramater do I need to change in this so it returns false. I have hit and tried but that didn't work. Thanks for your help.
Too much symbols, you can try this
- (BOOL) IsValidEmail:(NSString *)checkString {
BOOL sticterFilter = YES;
NSString *stricterFilterString = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = #".+#.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = sticterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
Try using this as regex
NSString *emailRegex = #"[A-Z0-9a-z._]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
the {2,4} especially validates that the ending characters should be alphabets and more than two in count

Remove Non-English Characters from NSString (Obj-C)

I have NSString like
Aavkar Complex, Opposite Gurukul, Drive-in Road, Ahmedabad,
àªà«àªàª°àª¾àª¤, India
so I want to consider only english characters and without
english characters are from above string so please give me any idea.
Thanks in advance.
Here is a little dirty example:
NSString *test = #"Olé, señor!";
NSMutableString *asciiCharacters = [NSMutableString string];
for (NSInteger i = 32; i < 127; i++) {
[asciiCharacters appendFormat:#"%c", i];
}
NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:asciiCharacters] invertedSet];
test = [[test componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] componentsJoinedByString:#""];
NSLog(#"%#", test); // Prints #"Ol, seor!"
try this:-
NSString *emailRegEx = #"[A-Za-z]";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
//Valid email address
NSString *textString=#"gagdaksdhaksdhaskdhasldhasldalasàªà«àªàª°àª¾àª¤dhwheqweuqweuqwe";
NSString *textFinalString=#"";
for (int i=0; i<[textString length]; i++) {
NSString *text2string=[textString substringWithRange:NSMakeRange(i,1)];
NSLog(#"%#",text2string);
if ([emailTest evaluateWithObject:text2string] == YES)
{
NSLog(#"yesenglishCharacter");
textFinalString=[textFinalString stringByAppendingString:text2string];
}
else {
NSLog(#"noenglishCharacter");
}
}
NSLog(#"textFinalString%#",textFinalString);
Here is some sample code using NSRange's to do this for a textfield, although the code should be easily adaptable to use for an array of NSString's. Hope that Helps!

NSPredicate use in iOS

Can anyone knows how to use NSPredicate for below format?
[Any letter][Any Number][Any letter][space][Any Number][Any letter][Any Number]
I want to validate string fot above fromat.
Thanks.
Use this format.
NSString *str1 = #"a8D 9k3";
NSString *str2 = #"a8 9k3";
NSString *testFormat = #"[a-zA-z][0-9][a-zA-z] [0-9][a-zA-z][0-9]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF matches %#", testFormat];
Following is valid:
BOOL isValid = [predicate evaluateWithObject:str1];
Following is invalid:
BOOL isValid = [predicate evaluateWithObject:str2];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF MATCHES[c] %#", #"[a-z][0-9][a-z] [0-9][a-z][0-9]"];
if ([pred evaluateWithObject:#"a3B 5C9"])
{
NSLog(#"It matches!");
}

Objective-C: How can I detect http URL's in a string?

Let's assume I have the string:
"I love visiting http://www.google.com"
How can I detect the token, http://www.google.com?
You can use NSDataDetectors These were added in iOS4 and are quite useful. You want to create a data detector with the NSTextCheckingTypeLink and let it do its thing.
NSString *testString = #"Hello http://google.com world";
NSDataDetector *detect = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [detect matchesInString:testString options:0 range:NSMakeRange(0, [testString length])];
NSLog(#"%#", matches);
You could do something like:
-(BOOL)textIsUrl:(NSString*)someString {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF MATCHES ^[-a-zA-Z0-9#:%_\\+.~#?&//=]{2,256}\\.[a-z]{2,4}\\b(\\/[-a-zA-Z0-9#:%_\\+.~#?&//=]*)?$"];
[predicate evaluateWithObject:someString];
}