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;

    }

  • 相关阅读:
    使用事件驱动代替定时任务
    MySql中的有条件插入 insert where
    Mac上“您没有权限来打开应用程序”(Big Sur)
    Java反编译反混淆神器
    Java实现开根号运算(不使用数组和String)
    使用vs code搭建Q#开发环境 (Mac)
    离散傅里叶变换DFT入门
    Java的nanoTime()方法
    Eslint提示const关键字被保留
    myBatis分页插件PageHelper的使用及源码详解
  • 原文地址:https://www.cnblogs.com/sgdkg/p/2825990.html
Copyright © 2011-2022 走看看