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;

    }

  • 相关阅读:
    [Eclipse]GEF入门系列(四、其他功能)
    [Eclipse]GEF入门系列(三、应用实例)
    [Eclipse]GEF入门系列(一、Draw2D)
    打开Win2000的自动补齐功能
    让URLConnection使用代理服务器
    [Eclipse]GEF入门系列(序)
    给表格的单元格增加编辑功能(In place edit)
    设置Eclipse RCP程序的外观和首选项
    利用winrar自动备份重要资料(续,经验技巧)
    终于换了新电脑
  • 原文地址:https://www.cnblogs.com/sgdkg/p/2825990.html
Copyright © 2011-2022 走看看