验证 uitextfield 中的电话号码 [英] Validating phone number in uitextfield

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

问题描述

我正在使用以下代码以以下格式在文本字段中显示电话号码

I am using the following code to display phone number in text field in the following format

格式为 123-456-7890

and the format is 123-456-7890

代码运行正常

代码如下

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


NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length]; 

if ([string isEqualToString:@""]) 
{
    return YES;
} 

if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound|| [string rangeOfString:@"-"].location != NSNotFound|| charCount > 12) {
    return NO;
}

if (charCount == 3 || charCount == 7) {
    newString = [newString stringByAppendingString:@"-"];
}

amountField.text = newString;

return NO;


}

我正在使用 UItextfield 委托方法.

I am using the UItextfield delegate method.

但是当我将文本字段编辑为-"时,这意味着(如果我尝试更改文本字段 123-456-7890 中的数字,如果我再次输入剩余数字,则不会验证该数字 123-456从这里它显示为 123-4567890

But when I am editing the text field up to "-" that mean (if i tried to change the number in text field 123-456-7890 it is not validating that mean 123-456 if i again enter the remaining number from here it is displaying as 123-4567890

任何人都可以帮助我如何验证这个东西

can any one please help me how to validate this thing

推荐答案

你有一个关于验证美国和国际电话号码的很好的教程

You have a good tutorial about validating both US and international phone numbers

http://blog.stevenlevithan.com/archives/validate-phone-number

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

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