UITextField输入仅允许数字


82

iPad没有iPhone / iPod那样的“数字键盘”。

我正在寻找如何限制用户键盘仅接受0到9的值。

我可以想象使用UITextField的“ shouldChangeCharactersInRange”,但是我不知道实现它的最佳方法。


我编写了一个教程,介绍如何使用可下载的项目源代码来完成此任务。在这里:xcodenoobies.blogspot.com/2013/12/...
GeneCode

Answers:


86

这样可以在SSN验证字段上解决问题,可以根据需要修改最大长度并删除if用于检查键盘类型的语句。

还有一种逻辑可以在用户键入而不是粘贴数据时抑制最大长度警报。

在此代码的上下文中,presentAlert()/presentAlert:只是一些基本功能,该功能使用传递的消息字符串来表示UIAlertController(或旧式UIAlertView)。

迅捷5

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
// object that will contain this code, because otherwise it would never be called.
//
// There are also some better stylistic approaches in Swift to avoid all the 
// nested statements, but I wanted to keep the styles similar to allow others 
// to contrast and compare between the two languages a little easier.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Handle backspace/delete
    guard !string.isEmpty else {

        // Backspace detected, allow text change, no need to process the text any further
        return true
    }

    // Input Validation
    // Prevent invalid character input, if keyboard is numberpad
    if textField.keyboardType == .numberPad {

        // Check for invalid input characters
        if CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) {

            // Present alert so the user knows what went wrong
            presentAlert("This field accepts only numeric entries.")

            // Invalid characters detected, disallow text change
            return false
        }
    }

    // Length Processing
    // Need to convert the NSRange to a Swift-appropriate type
    if let text = textField.text, let range = Range(range, in: text) {

        let proposedText = text.replacingCharacters(in: range, with: string)

        // Check proposed text length does not exceed max character count
        guard proposedText.count <= maxCharacters else {

            // Present alert if pasting text
            // easy: pasted data has a length greater than 1; who copy/pastes one character?
            if string.count > 1 {

                // Pasting text, present alert so the user knows what went wrong
                presentAlert("Paste failed: Maximum character count exceeded.")
            }

            // Character count exceeded, disallow text change
            return false
        }

        // Only enable the OK/submit button if they have entered all numbers for the last four
        // of their SSN (prevents early submissions/trips to authentication server, etc)
        answerButton.isEnabled = (proposedText.count == 4)
    }

    // Allow text change
    return true
}

目标C

// NOTE: This code assumes you have set the UITextField(s)'s delegate property to the 
// object that will contain this code, because otherwise it would never be called.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Handle backspace/delete
    if (!string.length)
    {
        // Backspace detected, allow text change, no need to process the text any further
        return YES;
    }

    // Input Validation
    // Prevent invalid character input, if keyboard is numberpad
    if (textField.keyboardType == UIKeyboardTypeNumberPad)
    {
        if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound)
        {
            [self presentAlert: @"This field accepts only numeric entries."];
            return NO;
        }
    }

    // Length Validation
    NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    // Check proposed text length does not exceed max character count
    if (proposedText.length > maxCharacters)
    {
        // Present alert if pasting text
        // easy: pasted data has a length greater than 1; who copy/pastes one character?
        if (string.length > 1)
        {
            // Pasting text, present alert so the user knows what went wrong
            [self presentAlert: @"Paste failed: Maximum character count exceeded."];
        }

        // Character count exceeded, disallow text change
        return NO;
    }

    // Only enable the OK/submit button if they have entered all numbers for the last four
    // of their SSN (prevents early submissions/trips to authentication server, etc)
    self.answerButton.enabled = (proposedText.length == maxCharacters);

    // Allow text change
    return YES;
}

2
谢谢!“如果键盘是数字键盘,则从输入中删除无效字符”部分有助于回答我的问题!
Demasterpl 2012年

@Gargo,该问题专门指出,允许的唯一值应该是0到9之间的数字。句点字符不在这些要求之内。为了允许使用句点字符,可以在此处查看Aje给出的答案。
Beltalowda 2014年

已经使用了它,但是留下了前导零的问题
Gargo 2014年

@Gargo,您可以使用类似于他检测其他句点字符的方法,并且在以下情况下仅对零字符返回yes:文本字段当前为空,如果插入点位于索引0且下一个字符为句点,或者如果插入点的索引大于现有句点字符的索引。至少那是我可能要检查以确保输入的零不会造成前导零问题的一种方式。
Beltalowda 2014年

26

您可以使用此代码在textField中仅允许数字。

在此之前,为textField设置委托

      textFieldName.delegate=self;

要么

      [textFieldName setDelegate:self];

比使用此代码只允许数字到textField

      - (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string {
//return yes or no after comparing the characters

      // allow backspace
      if (!string.length)
      {
           return YES;
      }

      ////for Decimal value start//////This code use use for allowing single decimal value
      //    if ([theTextField.text rangeOfString:@"."].location == NSNotFound)
      //    {
      //        if ([string isEqualToString:@"."]) {
      //            return YES;
      //        }
      //    }
      //    else
      //    {
      //        if ([[theTextField.text substringFromIndex:[theTextField.text rangeOfString:@"."].location] length]>2)   // this allow 2 digit after decimal 
      //        {
      //            return NO;
      //        }
      //    }
      ////for Decimal value End//////This code use use for allowing single decimal value

      // allow digit 0 to 9
      if ([string intValue])
      {
            return YES;
      }

      return NO;
    }

5
btw,对于其他使用此代码的用户,[string intValue]对于@“ 0”返回0-因此对于@“ 0”if ([string intValue])不满足。更好地使用if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
CharlesA 2015年

2
@".".intValue是0。@"0".intValue也是0。
Jaybo 2015年

为了在此处阐明其他注释:此代码不允许用户键入零(0)字符。
Beltalowda

22

尝试此操作以避免文本字段清除问题

斯威夫特3.0

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {
        return false
    }
    return true
}

迅捷4.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
        return false
    }
    return true
}

2
您可以简化委托方法,就在左侧return guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string))
hamsternik '18

我复制并粘贴到我的代码上,但是不起作用。如何连接并使其工作?
Yash Jain

首先设置textField委托(textField.delegate = self)并遵循UITextFieldDelegate协议。
SPatel

19

Swift代码的非常具体的步骤

您可以func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool通过实现UITextFieldDelegate协议来提供限制方法中文本字段输入的逻辑。

为了清楚起见,这些步骤假定您的情节提要中包含一个View Controller,该View Controller文本字段对象仅应接受数字。

  1. 为扩展的视图控制器创建一个自定义类UIViewController。通过在Xcode的Identity Inspector中设置自定义类的值,确保情节提要中的场景引用了自定义类。

    import UIKit
    class YourCustomController: UIViewController {
        override func viewDidLoad() {        
            super.viewDidLoad()
        }
    }
    
  2. 创建一个从场景的文本字段到自定义View Controller的插座。

    class YourCustomController: UIViewController {
        @IBOutlet weak var numberField: UITextField!
        ...
    }
    
  3. UITextFieldDelegate在自定义视图控制器中应用协议。

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
    }
    
  4. 在自定义视图控制器的viewDidLoad方法中,将文本字段的委托分配给自定义视图控制器类。

    override func viewDidLoad() {        
        super.viewDidLoad()
        numberField.delegate = self
    }
    
  5. 添加UITextFieldDelegatefunc textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool方法。

    由于numberField在上一步中将自定义视图控制器设为的委托,因此,每当用户在文本字段中输入字符时,都会调用此方法。如果您的方法返回,true则该字符将保留在文本字段中。如果您的方法返回,false则该字符将不会保留在文本字段中。

    string参数是由用户输入的字符。如果string可以将字符转换为,Int则它在0到9之间;否则,它为0。否则,它是一些非数字字符。

    class YourCustomController: UIViewController, UITextFieldDelegate {
        ...
        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
            return Int(string) != nil
        }
    }
    

(有关完整视图控制器的代码,请参见下文。)


仅数字文本字段的View Controller示例

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {        
        return Int(string) != nil
    }    
}

带有十进制文本字段的示例View Controller

如果要支持十进制数,请利用 NSNumberFormatter。有关差异,请参见代码注释。

import UIKit

class YourCustomController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var numberField: UITextField!

    private var formatter: NSNumberFormatter!

    override func viewDidLoad() {        
        super.viewDidLoad()       
        numberField.delegate = self

        // Initialize the formatter; minimum value is set to zero; style is Decimal. 
        formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        formatter.minimum = 0
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        // Combine the current text field value and the new string
        // character. If it conforms to the formatter's settings then
        // it is valid. If it doesn't then nil is returned and the
        // string character should not be allowed in the text field.         
        return formatter.numberFromString("\(textField.text)\(string)") != nil
    }    
}

2
这很好,但是做了一些小的调整,因为它不允许您在不检查空字符串的情况下删除字段中的任何内容。我还通过检查第一个字符(string ==“-” && range.location == 0)||来添加否定功能。string ==“” {return true} return string.toInt()!= nil
ickydime 2015年

return string.toInt() != nil 像魅力一样工作。谢谢!
CalZone 2015年

请注意,在Swift 2中,我不得不将其更改为return Int(string) != nil
nevster,2015年

@nevster感谢您的评论!我认为大多数Swift开发人员已经或将要迁移到Swift 2及更高版本。因此,我更新了答案以符合Swift 2的字符串到整数转换。
whyceewhite15年

7
我必须做的另一项更改-删除键不再起作用!所以我将其更改为return string == "" || Int(string) != nil
nevster

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

    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterNoStyle];

    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];

    if (number)
        return YES;
    else
       return NO;
}

1
这对于需要更改正确的newString的分数正好工作:NSString * newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
Idali 2015年

7

我应用了它并且有效!

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Check for non-numeric characters
NSUInteger lengthOfString = string.length;
for (NSInteger index = 0; index < lengthOfString; index++) {
    unichar character = [string characterAtIndex:index];
    if (character < 48) return NO; // 48 unichar for 0
    if (character > 57) return NO; // 57 unichar for 9
}
// Check total length for restrict user
NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
if (proposedNewLength > 6)
    return YES;
return YES;                                                                                                                                     
}

1
要添加“。”,请替换if (character < 48) return NO; // 48 unichar for 0 if (character > 57) return NO; // 57 unichar for 9if ((character < 48 || character > 57) && character != 46)我,另外建议您将character数字与十六进制表示形式进行比较,因为在这些情况下,十六进制最常用。即if ((character < 0x30 || character > 0x39) && character != 0x2E)
雅各布- [R

2
NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
    if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
        return NO;
    }

2
Works fine for me :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound) && !(range.length==1 && string.length==0)) {
            return NO;
        }
        return YES;
    }

1

在Swift中:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return string.isEmpty || Int(string) != nil
    }

1

迅捷5

    //MARK:- UITextFieldDelegate

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let allowedCharacters = "1234567890"
    let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
    let typedCharcterSet = CharacterSet(charactersIn: string)
    return allowedCharcterSet.isSuperset(of: typedCharcterSet)
}

您现在只能点击1234567890


您如何实现呢?仅创建此函数将不会将其连接到UITextfield
Yash Jain

0

保留内部表示形式与众不同的表示数据。有一种更简单的方法。让我们NSNumberFormatter做的工作:

 NSNumberFormatter* ns = [[NSNumberFormatter alloc] init];
 ns.numberStyle = NSNumberFormatterDecimalStyle;
 [ns setMaximumFractionDigits:2];
 // This is your internal representation of the localized number
 double a = [[ns numberFromString:self.textIVA.text] doubleValue]];

[mylabel setText:[NSString stringWithFormat:@"€ %@",
     [NSNumberFormatter localizedStringFromNumber:
                          [NSNumber numberWithDouble:a]
                                      numberStyle:NSNumberFormatterDecimalStyle]]];

0

如果使用我的规范模式,则代码如下所示

textField.delegate = self

lazy var specification: Specification = {
    return RegularExpressionSpecification(pattern: "^(|0|[1-9]\\d{0,6})$")
}()

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let textFieldString: NSString = textField.text ?? ""
    let s = textFieldString.stringByReplacingCharactersInRange(range, withString:string)
    return specification.isSatisfiedBy(s)
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let s = textField.text ?? ""
    let isTextValid = specification.isSatisfiedBy(s)
    if isTextValid {
        textField.resignFirstResponder()
    }
    return false
}

我如何将UITextfield限制为仅接收数字并限制6到8之间的数字量?
马可·阿尔梅达

嗨,@ MarcoAlmeida看看我的框架SwiftyFORM,它可以实时验证文本,github.com
neoneye/

0

我修改了@iDev的答案以用于数字和“。”:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
     // Check for non-numeric characters
     NSUInteger lengthOfString = string.length;
     for (NSInteger index = 0; index < lengthOfString; index++) {
         unichar character = [string characterAtIndex:index];
         if ((character < 48) && (character != 46)) return NO; 
         // 48 unichar for 0, and 46 unichar for point
         if (character > 57) return NO; 
         // 57 unichar for 9
     }
     // Check for total length
     NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
     if (proposedNewLength > 6)
         return YES;
     return YES; 
 }

0

迅捷3

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField==yourTextFieldOutlet {
                if(CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: yourTextFieldOutlet.text!))){
//if numbers only, then your code here
                }
                else{
                showAlert(title: "Error",message: "Enter Number only",type: "failure")
                }
            }
    return true
    }

-1

使用此代码:

NSString* val = [[textField text] stringByReplacingCharactersInRange:range withString:string];
NSCharacterSet *allowedCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
if ([[string componentsSeparatedByCharactersInSet:[allowedCharacterSet invertedSet]] count] > 1 || [val length] > 5) {
    return NO;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.