Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何验证允许的文本字段。仅在文本字段中使用一次_Iphone_Objective C_Ios - Fatal编程技术网

Iphone 如何验证允许的文本字段。仅在文本字段中使用一次

Iphone 如何验证允许的文本字段。仅在文本字段中使用一次,iphone,objective-c,ios,Iphone,Objective C,Ios,我的应用程序中有4个文本字段 我将textfield验证为textfield allow0,1,….9和,因此我将代码写为fallows - (IBAction) textfield:(id)sender { if ([textfield.text length] > 0) { if ([textfield.text length] > 10){ textfield.text = [textfield.text s

我的应用程序中有4个文本字段

我将textfield验证为textfield allow
0,1,….9
,因此我将代码写为fallows

- (IBAction) textfield:(id)sender {

    if ([textfield.text length] > 0) {
        if ([textfield.text length] > 10){          
            textfield.text = [textfield.text substringWithRange:NSMakeRange(0, 10)];
    }
        else {
            //retrieve last character input from texfield           
            int I01 = [homevalue.text length];
            int Char01 = [homevalue.text characterAtIndex:I01-1];
            //check and accept input if last character is a number from 0 to 9
            if ( (Char01 < 46) || (Char01 > 57) || (Char01 == 47) ) {

                if (I01 == 1) {
                    textfield.text = nil;
                }
                else {

                    textfield.text = [homevalue.text substringWithRange:NSMakeRange(0, I01-1)];
                }
            }

        }
    }

} 
-(iAction)文本字段:(id)发件人{
如果([textfield.text length]>0){
如果([textfield.text length]>10{
textfield.text=[textfield.text substringWithRange:NSMakeRange(0,10)];
}
否则{
//从texfield检索最后一个字符输入
int I01=[homevalue.text length];
int Char01=[homevalue.text characterAtIndex:I01-1];
//如果最后一个字符是0到9之间的数字,请检查并接受输入
如果((Char01<46)| |(Char01>57)| |(Char01==47)){
如果(I01==1){
textfield.text=nil;
}
否则{
textfield.text=[homevalue.text substringWithRange:NSMakeRange(0,I01-1)];
}
}
}
}
} 
它工作正常,现在我需要验证
只允许在文本字段中使用一次

例:123.45

根据我的代码,如果我再次放置
,这是允许的。 例:123.45.678

但是一旦我把它放进去就不允许了,那个文本字段是不允许的

埃德:123.45678

我怎么能做到这一点

谁能帮我一下吗


先谢谢你

你可以用这个方法


-(NSRange)rangeOfCharacterFromSet:(NSCharacterSet*)aSet选项:(NSStringCompareOptions)掩码范围:(NSRange)aRange


在textfield.text上,仅将其作为NSString

对以“23.67”等数字开头的文本尝试此谓词

如果您想在第一个位置允许“.”如“.95”,请使用以下正则表达式

NSString *decimalRegex = @"[0-9]*([.]([0-9]+)?)?"; //@"[0-9]*[.][0-9]+";
你的代码应该是这样的

- (IBAction)textfield:(id)sender {

    int textLength = [[textfield text] length];

    if (textLength > 0) {

        NSString *decimalRegex = @"[0-9]+([.]([0-9]+)?)?"; //@"[0-9]+[.][0-9]+";
        NSPredicate *decimalTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", decimalRegex]; 
        BOOL isValidDecimal = [decimalTest evaluateWithObject:[textField text]];

        if (!isValidDecimal) {

            NSString *text = [[textField text] substringToIndex:textLength - 1];
            [textfield setText:text] 
        }
    } 
}

我想这应该管用!试试看

Mahesh你能把问题说得更清楚些吗?很难说你的要求是什么。你应该把它放在textfield:(id)sender方法中。我已经编辑了我的答案。试试看。不,它不返回任何内容,如果我使用此代码,每次文本更改时都会调用textfieldtextfield:(id)sender方法,但不会在其中打印任何内容?如果是,将正则表达式更改为@“[0-9]+([.][0-9]?)?”。我现在已经编辑了我的代码。
- (IBAction)textfield:(id)sender {

    int textLength = [[textfield text] length];

    if (textLength > 0) {

        NSString *decimalRegex = @"[0-9]+([.]([0-9]+)?)?"; //@"[0-9]+[.][0-9]+";
        NSPredicate *decimalTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", decimalRegex]; 
        BOOL isValidDecimal = [decimalTest evaluateWithObject:[textField text]];

        if (!isValidDecimal) {

            NSString *text = [[textField text] substringToIndex:textLength - 1];
            [textfield setText:text] 
        }
    } 
}