Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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
Ios 验证用户输入号码_Ios_Iphone_Validation_Nsnumberformatter - Fatal编程技术网

Ios 验证用户输入号码

Ios 验证用户输入号码,ios,iphone,validation,nsnumberformatter,Ios,Iphone,Validation,Nsnumberformatter,在我的应用程序中,我只想允许用户以“1111.11”格式输入十进制数。这意味着,当用户在文本框中编辑时,我想检查最多4位整数和最多2位十进制数。如果用户输入错误,则不允许在文本框中输入。我已在中编写了代码: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 方法,但我并没有得到想要的结果。这是我的

在我的应用程序中,我只想允许用户以“1111.11”格式输入十进制数。这意味着,当用户在文本框中编辑时,我想检查最多4位整数和最多2位十进制数。如果用户输入错误,则不允许在文本框中输入。我已在中编写了代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
方法,但我并没有得到想要的结果。这是我的密码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == txtFund)
{
    double num;
        long iPart;
        double fPart;
            num = [textField.text doubleValue];
            iPart = (long) num;
            fPart = num - iPart;
        NSLog(@"1 %ld %f",iPart,fPart);
        NSString *integerPart=[NSString stringWithFormat:@"%ld",iPart];

            if([integerPart length]>=4){
                if([string isEqualToString:@"."]){

                }
                else{
                return NO;
                }
            }
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];
    if (numberOfMatches == 0)
        return NO;
}

return YES;
 }

通过这段代码,我可以实现像“4444”一样的输出。但是它是不允许的,因为它会在第二部分转到其他部分,如果您可以使用textfield textfield shouldendediting方法签出

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
    // getting the textfield text 
    // Separating the textfield value by (.)
    NSArray *arr=[textField.text componentsSeparatedByString:@"."];
    // Checking the textfiled text before dot
    NSLog(@"arr lenth @0==%i",[[arr objectAtIndex:0] length]);
    // Checking the textfiled text after dot
    NSLog(@"arr lenth @1==%i",[[arr objectAtIndex:1] length]);

   if([[arr objectAtIndex:0] length]>4)
   {
    // Comes here if textfield text value before dot is greater then 4 digits
    }

     if([[arr objectAtIndex:1] length]>2)
   {
      // Comes here if textfield text value after dot is greater then 2 digits
    }
 }

您可以使用textfield textFieldShouldEndEditing方法签出

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
    // getting the textfield text 
    // Separating the textfield value by (.)
    NSArray *arr=[textField.text componentsSeparatedByString:@"."];
    // Checking the textfiled text before dot
    NSLog(@"arr lenth @0==%i",[[arr objectAtIndex:0] length]);
    // Checking the textfiled text after dot
    NSLog(@"arr lenth @1==%i",[[arr objectAtIndex:1] length]);

   if([[arr objectAtIndex:0] length]>4)
   {
    // Comes here if textfield text value before dot is greater then 4 digits
    }

     if([[arr objectAtIndex:1] length]>2)
   {
      // Comes here if textfield text value after dot is greater then 2 digits
    }
 }

而不是用户键入。十进制符号,您可以通过编程方式插入一个,并允许用户继续。请尝试以下代码:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //NSLog(@"%i", textField.text.length);

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c])
            {
                return NO;
            }
            if(textField.text.length == 4) // Place a '.' when the text length is 4
            {
              textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"."];
            }
            if(textField.text.length >=7) // Restrict the user to enter only 7 digits incl. the dot
            {
                return NO;
            }
        }
        return YES;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
//NSLog(@“%i”,textField.text.length);
NSCharacterSet*myCharSet=[NSCharacterSet characterSetWithCharactersInString:@“0123456789”];
对于(int i=0;i<[字符串长度];i++){
unichar c=[string characterAtIndex:i];
如果(![myCharSet成员:c])
{
返回否;
}
if(textField.text.length==4)//当文本长度为4时,放置一个“.”
{
textField.text=[textField.text stringByReplacingCharactersInRange:range with string:@.”;
}
if(textField.text.length>=7)//限制用户仅输入包括点在内的7位数字
{
返回否;
}
}
返回YES;
}
更新:(如果您确实希望用户键入点,请尝试以下代码)

-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
//NSLog(@“%i”,textField.text.length);
NSCharacterSet*myCharSet=[NSCharacterSet characterSetWithCharactersInString:@“0123456789.”;
对于(int i=0;i<[字符串长度];i++){
unichar c=[string characterAtIndex:i];
如果(![myCharSet成员:c])
{
返回否;
}
如果(textField.text.length==4&&(![string IsequalString:@“)/)检查第5个字符,如果不是“.”,则不允许用户键入。。
{
返回否;
//textField.text=[textField.text stringByReplacingCharactersInRange:range with string:@.”;
}
if(textField.text.length>=7)//限制用户仅输入包括点在内的7位数字
{
返回否;
}
}
返回YES;
}

而不是用户键入。十进制符号,您可以通过编程方式插入一个,并允许用户继续。请尝试以下代码:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //NSLog(@"%i", textField.text.length);

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c])
            {
                return NO;
            }
            if(textField.text.length == 4) // Place a '.' when the text length is 4
            {
              textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"."];
            }
            if(textField.text.length >=7) // Restrict the user to enter only 7 digits incl. the dot
            {
                return NO;
            }
        }
        return YES;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
//NSLog(@“%i”,textField.text.length);
NSCharacterSet*myCharSet=[NSCharacterSet characterSetWithCharactersInString:@“0123456789”];
对于(int i=0;i<[字符串长度];i++){
unichar c=[string characterAtIndex:i];
如果(![myCharSet成员:c])
{
返回否;
}
if(textField.text.length==4)//当文本长度为4时,放置一个“.”
{
textField.text=[textField.text stringByReplacingCharactersInRange:range with string:@.”;
}
if(textField.text.length>=7)//限制用户仅输入包括点在内的7位数字
{
返回否;
}
}
返回YES;
}
更新:(如果您确实希望用户键入点,请尝试以下代码)

-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
//NSLog(@“%i”,textField.text.length);
NSCharacterSet*myCharSet=[NSCharacterSet characterSetWithCharactersInString:@“0123456789.”;
对于(int i=0;i<[字符串长度];i++){
unichar c=[string characterAtIndex:i];
如果(![myCharSet成员:c])
{
返回否;
}
如果(textField.text.length==4&&(![string IsequalString:@“)/)检查第5个字符,如果不是“.”,则不允许用户键入。。
{
返回否;
//textField.text=[textField.text stringByReplacingCharactersInRange:range with string:@.”;
}
if(textField.text.length>=7)//限制用户仅输入包括点在内的7位数字
{
返回否;
}
}
返回YES;
}

此代码应该符合您的要求

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^([0-9]{1,4})?(\\.([0-9]{1,2})?)?$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];
    if (numberOfMatches == 0)
        return NO;

    return YES;
}

此代码应该符合您的要求

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^([0-9]{1,4})?(\\.([0-9]{1,2})?)?$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];
    if (numberOfMatches == 0)
        return NO;

    return YES;
}

但此方法将在textfield结束编辑后调用。但我想在textfield中检查并替换为用户iput,但此方法将在textfield结束编辑后调用。但我想在textfield中检查并替换为用户iput查看我的更新答案..查看我的更新答案。。