美文网首页
限制输入内容

限制输入内容

作者: 把疯狂进行到底 | 来源:发表于2016-06-24 18:02 被阅读0次

    1、限制输入的为汉字和字母

    //正则表达式

    #define NMUBERS @"0123456789./*-+~!@#$%^&()_+-=,./;'[]{}:<>?`"

    NSCharacterSet *cs;

    cs = [[NSCharacterSet characterSetWithCharactersInString:NMUBERS] 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];

    //若输入的不是汉字或者字母删除输入的内容

    textField.text = nil;//return NO 时不能删除,

    }

    return YES;

    2、#define NUMBERS @"0123456789."//只允许输入数字或者点号

    不同的限制改变不同的表达式即可

    相关文章

      网友评论

          本文标题:限制输入内容

          本文链接:https://www.haomeiwen.com/subject/dhnodttx.html