Search code examples
iosobjective-cvalidationnsstringnsrange

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 
   //}

}

Solution

  • 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];
    }