仅允许UITextField输入的Numbers [英] Allow only Numbers for UITextField input

查看:145
本文介绍了仅允许UITextField输入的Numbers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPad没有像iPhone / iPod那样的Numpad键盘。



我想找到如何限制用户的键盘接受0到9的值。



我会想象使用UITextField的shouldChangeCharactersInRange,但我不知道实现它的最佳方法。

解决方案

这就是我在SSN验证字段上处理问题的方法,您可以修改最大长度并删除 if 如果需要,可以检查键盘类型的语句。



当用户键入而不是粘贴数据时,还有一个逻辑可以抑制最大长度警报。 / p>

在此代码的上下文中, BasicAlert() #define 使用传递的标题和消息字符串只显示 UIAlertView UIAlertController 的宏。

  //注意:此代码假设您已设置UITextField(s)的del将属性转换为包含此代码的对象,否则永远不会被调用。 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//允许退格
if(!string。长度)
{
返回YES;
}

//防止无效字符输入,如果键盘是numberpad
if(textField.keyboardType == UIKeyboardTypeNumberPad)
{
if([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet] .invertedSet] .location!= NSNotFound)
{
// BasicAlert(@,@此字段仅接受数字条目。);
返回NO;
}
}

//验证最大长度未超过
NSString * proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

if(proposedText.length> 4)// 4被选为SSN验证
{
//仅在用户键入$ b $时才抑制最大长度消息b // easy:粘贴的数据长度大于1;谁复制/粘贴一个字符?
if(string.length> 1)
{
// BasicAlert(@,@此字段最多接受4个字符。);
}

返回NO;
}

//只有在他们输入了最后四个SSN的所有数字时才启用OK / submit按钮(防止早期提交/前往认证服务器)
self .answerButton.enabled =(proposedText.length == 4);

返回YES;
}


The iPad does not have a "Numpad" keyboard like the iPhone/iPod does.

I'm looking to find how I can restrict the user's keyboard to only accept values 0 through 9.

I would imagine using UITextField's "shouldChangeCharactersInRange" but I don't know the best way to implement it.

解决方案

This is how I handled the problem on a SSN verification field, you can modify the max length and remove the if statement checking for keyboard type if you need to.

There is also logic to suppress the max length alerts when the user is typing as opposed to pasting data.

In the context of this code, BasicAlert() is a #define macro that simply shows a UIAlertView or UIAlertController using the title and message strings passed.

// 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
{
    // allow backspace
    if (!string.length)
    {
        return YES;
    }

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

    // verify max length has not been exceeded
    NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if (proposedText.length > 4) // 4 was chosen for SSN verification
    {
        // suppress the max length message only when the user is typing
        // easy: pasted data has a length greater than 1; who copy/pastes one character?
        if (string.length > 1)
        {
            // BasicAlert(@"", @"This field accepts a maximum of 4 characters.");
        }

        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)
    self.answerButton.enabled = (proposedText.length == 4);

    return YES;
}

这篇关于仅允许UITextField输入的Numbers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆