Pages

Sunday 21 December 2014

TextField Validation for iOS



I found that custom class for text filed  validation in iOS ,i will explain you in shortly.

demo code is available free on github, you can download URL:https://github.com/dhawaldawar/TextFieldValidator

Follow the steps to add text filed validatore 

1. Include “TextFieldValidator.h” & “TextFieldValidtor.m” file in your project.

2. Add error icon file (image file)in project (you can find this image file from that demo and that class file as well).

3. if you added text filed control in your project , then you  must have to assign the custom class to it as "TextFieldValidator" (in property  .and create outlet or create instance programmatically.

4.Must assign text filed to “presentInView” . If you are managing text field from storyboard/xib then you can directly assign outlet of “presentInView” to any view.

5
below validations regex to all above textfields
// Add regex for validating characters limit
[txtUserName
 addRegx:@”^.{3,10}$” withMsg:@”User name charaters limit should be come between 3-10″];
// Add regex for validating alpha numeric characters
[txtUserName addRegx:@”[A-Za-z0-9]{3,10}” withMsg:@”Only alpha numeric characters are allowed.”];
// Add regex for validating email id
[txtEmail addRegx:@”[A-Z0-9a-z._%+-]{3,}+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}”withMsg:@”Enter valid email.”];
// Add regex for validating characters limit[txtPassword addRegx:@”^.{6,50}$” withMsg:@”Password characters limit should be come between 6-20″];
// Add regex for validating alpha numeric characters[txtPassword addRegx:@”[A-Za-z0-9]{6,20}” withMsg:@”Password must contain alpha numeric characters.”];
// Add regex for validating confirm password text with the password field   
[txtConfirmPass addConfirmValidationTo:txtPassword withMsg:@”Confirm password didn’t match.”];
// Add regex for validating phone number
[txtPhone addRegx:@”[0-9]{3}\\-[0-9]{3}\\-[0-9]{4}” withMsg:@”Phone number must be in proper format (eg. ###-###-####)”];
// To set phone number is not mandatory
txtPhone.isMandatory=NO;
Now if tap on “Submit” button then error will show for text fields according to the validation that failed
if([txtUserName validate] & [txtEmail validate] & [txtPassword validate] & [txtConfirmPass validate] & [txtPhone validate]){
       //Success and now you can redirect the to another view
}



 For more info , get in touch with this blog. and i am very thankful to  Dhawal Dawar for writing this article and coding this  ( http://www.thinkotech.com )

No comments:

Post a Comment