I'm really new to ios and i'm trying to write a function that recieves 4 string,
Heres what I try:
this is in my h file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
this is in my m file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
{
NSString *emailRegex = #"[A-Z0-9A-z._%+- ]+#[A-Za-z0-0.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",emailRegex];
return YES;
}
the errors is get are that iam using an undeclared identifier
on the emailRegex
and on the emaitTest
Basicly all i'm trying to do is declare a method that recieves 4 strings:
fullname
email
username
password
and implement it.
any help would be good.
good day.
It should be as :
-(BOOL)validateFullName:(NSString*)fullName email:(NSString*)email userName:(NSString*)username withPassword:(NSString*)password;
For Better readability :
-(BOOL)validateFullName:(NSString*)fullName
email:(NSString*)email
userName:(NSString*)username
password:(NSString*)password;
*Edit: Naming convention was not best, so renamed.
Related
I am doing an NSLog of an array but instead of data it shows the following values. I do not know how to fix this issue and get the values from the array
if(!surveyQuestions){
surveyQuestions=[[NSMutableArray alloc]init];
}
Total Survey Questions 3
2012-07-31 08:54:53.555 SQL[442:9203] SurveyQuestions (
"<QuestionData: 0x4da10f0>",
"<QuestionData: 0x4b9f120>",
"<QuestionData: 0x4ba42e0>"
)
I'm not sure what you're trying to do but: it's certain that the poor array object has no idea what and how your own custom class does, its best possibility to print an instance of your class is to call its description method, which you see, and which is not really helpful. You maybe want to do two things:
I. If you only want to print your objects like this, override the description method of your class and use some format string (given that you haven't written a single line of code I have to fall back to guess):
- (NSString *)description
{
return [NSString stringWithFormat:#"Name: %#, address: %#", self.name, self.address];
}
II. If you want to use the data of your class elsewhere, you probably want to loop through its properties manually:
for (QuestionData *d in surveyQuestions)
{
NSLog(#"%#", d.name);
// etc.
}
You need to do something like this:
NSArray *theArray = [[NSArray alloc] initWith...];
NSLog(#"array contents: %#", theArray);
I'm making the pattern to check the user can only enter number and string. Please anyone can help me or give some reference. Following is my code. Thnks in advance.
NSString *regEx =
#"(?:[a-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])+)\\])";
NSString *strRoadworthyGet=#"vinod123";
NSPredicate *matchPattern =[NSPredicate predicateWithFormat:#"SELF MATCHES %#",regEx];
BOOL resultMatch =[matchPattern evaluateWithObject:strRoadworthyGet];
if(!resultMatch)
NSLog(#"invalid pattern");
You can use NSCharacterSet's alphanumeric method to return check for Letters, Marks and Numbers.
If you want to go for RegularExpression, you can use
NSString *regEx = #"^[a-zA-Z0-9]*$";
Defining legal input characters
I want to check whether a string contains any of the substrings that I place in an array. Basically, I want to search the extensions of a file, and if the file is an "image", i want certain code to execute. The only way I can think of categorizing the file as an "Image" without downloading the file is through the substring in a string method. This is my code so far:
NSString *last5Chars = [folderName substringFromIndex: [folderName length] - 5];
NSRange textRangepdf;
textRangepdf =[last5Chars rangeOfString:#"pdf"];
if(textRangepdf.location != NSNotFound)
{
[self.itemType addObject:#"PDF.png"];
}
Is it possible to do this where I can check if last5Chars contains #"jpg" or #"gif" of #"png" etc...?? Thanks for helping!
NSString *fileName;
NSArray *imgExtArray; // put your file extensions in here
BOOL isImage = [imgExtArray containsObject:[fileName pathExtension]];
[folderName hasSuffix:#".jpg"] || [folderName hasSuffix:#".gif"]
obviously you can put it into a loop, if you have a whole arrayful.
Rather than mess about with ranges and suffixes, NSString has a method that treats an NSString as a path and returns an extension, it's called pathExtension.
Have a look in the NSString Documentation
Once you get the extension you can check it against whatever strings you want.
I was trying to validate URL links using regex, but not all the links are being completely identified. Can you please help me out?
I want the links to follow the pattern:
http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214
Assuming you are looking for a regular expression to match urls with a specific pattern:
You can use something like this to match http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214:
http://.*?/[a-zA-z]+/content.aspx\?menu id=\d+?&id=\d+
Regex for url with port in Objective c
It works well if don't have port number.
-(BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx = #"^(http|https|ftp)\://(([a-zA-Z0-9-.]+\.[a-zA-Z]{2,3})|([0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d))(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._\?\,\'/\+&%\$#\=~])*[^.\,)(\s]$";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
I am an old developer but new to Objective-C and iPhone development. I am looking for the best way to do form validation for values entered. I have been googling for a while and can't find any good code, but might be using the wrong key worrds, etc... I am looking for things like catching for empty string, numeric validation, date validation, etc... where it catches this either while the user is entering data (input mask type) or on the lost focus of the control.
If you can point me to some good resources that would be great!
Thanks!
Simon.
I don't know particular resources but you could begin usin NSPredicate to validate fields with regular expressions. For example, for mail validation
BOOL result;
NSString* emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate* emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
result = [emailTest evaluateWithObject:anEmailAddress];
The emailRegex suggested by Espuz has a slight problem. The range operator "-" within the [] brackets should be escaped otherwise emails having "-" character are returned as invalid.
I am using the following regex string for pattern matching that allows "_", "-" and "." characters in the address:
NSString *emailRegex = #"[a-zA-Z0-9.\\-_]{2,32}#[a-zA-Z0-9.\\-_]{2,32}\.[A-Za-z]{2,4}";
NSPredicate *regExPredicate =
[NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL validEmail = [regExPredicate evaluateWithObject:txtField.text];