uitextfield 只能输入数字,正则表达式

 1、定义一个要保留的常量 #define NUMBERS @"0123456789.\n"

 2、实现delegate

3、

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSCharacterSet *cs;

    cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS] invertedSet];

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    BOOL basicTest = [string isEqualToString:filtered];

    if(!basicTest)

    {

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"

                                                        message:@"请输入数字"

                                                       delegate:nil

                                              cancelButtonTitle:@"确定"

                                              otherButtonTitles:nil];

        

        [alert show];

        return NO;

    }

    else if (textField.text.length>10)

    {

        

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"

                                                        message:@"输入格式错误"

                                                       delegate:nil

                                              cancelButtonTitle:@"确定"

                                              otherButtonTitles:nil];

        

        [alert show];

        return NO;

    }

    

    returnYES;

}

原文地址:https://www.cnblogs.com/sgdkg/p/2825990.html