如何使文本字段只接受iphone中的4位数或数字

时间:2013-08-31 09:12:30

标签: iphone ios objective-c uitextfield

我有一个应用,我想在UITextField中输入一个数值。但我想只允许输入4位数字。因此 1234 有效,但无法输入 12345 。知道我怎么能修改它只接受限于 4 数字的数值?

4 个答案:

答案 0 :(得分:16)

示例代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    int length = [currentString length];
    if (length > 4) {
       return NO;
    }
    return YES;
}

答案 1 :(得分:3)

  

Swift 3.0 Xcode 8.3.3 ......

首先UITextField设置委托,然后输入此代码......

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let textstring = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    let length = textstring.characters.count
    if length > 4 {
        return false
    }
    return true
}
  

目标C代码......

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      {
        NSString *textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
        int length = [textstring length];
        if (length > 4) {
           return NO;
        }
            return YES;
      }

答案 2 :(得分:2)

下面的代码帮助我设置了仅在文本字段中输入2位和4位的约束。输入两位数后,焦点自动移动到下一个文本字段。

Screenshot

override func viewDidLoad() {
  super.viewDidLoad()

   // Set an action on EditingChanged event of textfield
  txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
}

func textFieldDidChange(textField: UITextField){

  let text = textField.text

   if textField.tag == 101 { // Set tag to textfield (if multiple) during design time
      if text?.utf16.count==2 {
        txtCardDetails4.becomeFirstResponder() // Move to next text field
       }
   }
}

func textFieldDidBeginEditing(textField: UITextField) {
   textField.becomeFirstResponder()
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;
}


//User can enter two digits in textfield with tag 101 and four digits in textfield with tag 102
  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
      if let text = textField.text {

        let newStr = (text as NSString)
                .stringByReplacingCharactersInRange(range, withString: string)
            if newStr.isEmpty {
                return true
            }
            let intvalue = Int(newStr)

            if textField.tag == 101 { print("101")
                 return (intvalue >= 0 && intvalue <= 99) ? true : false
            }
            else if textField.tag == 102 { print("102")
                 return (intvalue >= 0 && intvalue <= 9999) ? true : false
            }

        }
        return true
    }

答案 3 :(得分:0)

已编辑:

首先更改textFiled的keyBoard类型UIKeyboardTypeNumberPad,因此键盘只有数字/数字。

并提供您要UITextField标记 (仅输入4位数字) ,例如< / p>

textField.tag = 101; /// this is for, if you have multiple textFiled then it recognize that which textFiled have limit of 4 number ;

使用以下方法的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.tag != 101)
      return YES;

    if(string.length == 0)
      return YES;

    if(textField.text.length == 4)
        return NO;

    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

    if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound)
    {
        return NO;
    }
    return YES;
}