zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    无法与服务器建立安全链接
    OpenGL基础探究
    百度语音识别接入遇到的问题
    语音音频相关
    Gif动态图UIImage
    替换图像的某种色值
    错误
    各种须知分类等
    动画支持的一些keypath
    X pgAdmin4 重置布局 页面关不掉,要疯掉了
  • 原文地址:https://www.cnblogs.com/sgdkg/p/2825990.html
Copyright © 2011-2022 走看看