iOS TextField Validation

前端 未结 3 2057
南方客
南方客 2021-02-09 07:50

I need a way to ensure that phone numbers have 10 digits with no other characters ie () - and make sure that email addresses are valid emails (formatted correctly).

Is t

3条回答
  •  难免孤独
    2021-02-09 08:39

    Here's Simple Example for UITextField Validation while type in keyboard other characters not displaying

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
    //UITextField *tf_phonenumber,*tf_userid;
    if (textField.text<=10) {
    char c=*[string UTF8String];
    if (tf_phonenumber==textField)      //PhoneNumber /Mobile Number
    {
        if ((c>='0' && c<='9')||(c==nil)) {
            return YES;
        }
        else
            return NO;
    }
    if (tf_userid==textField)           //UserID validation
    {
        if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c==' ')||(c==nil)) {
            return YES;
        }
        else
            return NO;
    }
        return YES;
    }
    else{
        return NO;
    }
    }
    

提交回复
热议问题