iOS TextField验证 [英] iOS TextField Validation

查看:162
本文介绍了iOS TextField验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来确保电话号码有10位数而没有其他字符,即() - 并确保电子邮件地址是有效的电子邮件(格式正确)。



<是否有任何库不能使我这么容易,所以我不必编写正则表达式。

解决方案

这将检查UITextField是否有正确的10位数或更少的电子邮件和电话号码。

将此方法添加到 textFields delegate 然后检查是否应该添加要更改的字符。

返回取决于文本字段,当前有多少个字符在其中,以及它想要添加的字符:

  #define ALPHA @ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk lmnopqrstuvwxyz
#define NUMERIC @1234567890
#define ALPHA_NUMERIC ALPHA NUMERIC

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString :( NSString *)string {
NSCharacterSet * unacceptedInput = nil;
switch(textField.tag){
//假设EMAIL_TextField.tag == 1001
case 1001:
if([[textField.text componentsSeparatedByString:@@] count ]> 1)
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@.-]] invertedSet];
else
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@。!#$%&'* + - / =?^ _`{|}〜@]] invertedSet];
休息;
//假设PHONE_textField.tag == 1002
case 1002:
if(textField.text.length + string.length> 10){
return NO;
}
unacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
休息;
默认值:
unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
休息;
}
return([[strings componentsSeparatedByCharactersInSet:unacceptedInput] count]< = 1);
}






另外,看看这些2篇文章:

在iPhone上自动格式化电话号码UITextField

PhoneNumberFormatter


I need a way to ensure that phone numbers have 10 digits with no other characters ie () - and make sure that email addresses are valid emails (formatted correctly).

Is there any library that can't make this easy for me so I don't have to write regular expressions.

解决方案

This will check a UITextField for a proper email and phone number of 10 digits or less.
Add this method to the textFields delegate then check if the characters it is about to change should be added or not.
Return YES or NO depending on the text field, how many characters are currently in it, and what characters it wants to add:

#define ALPHA                   @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMERIC                 @"1234567890"
#define ALPHA_NUMERIC           ALPHA NUMERIC

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *unacceptedInput = nil;
    switch (textField.tag) {
        // Assuming EMAIL_TextField.tag == 1001
        case 1001:
            if ([[textField.text componentsSeparatedByString:@"@"] count] > 1)
                unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet];
            else
                unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&'*+-/=?^_`{|}~@"]] invertedSet];
            break;
        // Assuming PHONE_textField.tag == 1002
        case 1002:
            if (textField.text.length + string.length > 10) {
                return NO;
            }
            unacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            break;
        default:
            unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
            break;
    }
    return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}  


Also, check out these 2 articles:
Auto-formatting phone number UITextField on the iPhone
PhoneNumberFormatter.

这篇关于iOS TextField验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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