如何在文本字段中对用户在iPhone中输入的电话号码进行验证? [英] How to perform validation on textfield for phone number entered by user in iPhone?
问题描述
我有一个应用程序,我有一个文本字段,用户输入他的手机号码,包括他的国家代码。要输入的手机号码的格式为+ 91-9884715715。当用户输入他/她的手机号码时,应首先验证用户输入的第一个值是+,然后在+之后输入的数字不应小于0。
I have an application where I have I a textfield where user enters his mobile number including his country code. The format of the mobile number to be entered is +91-9884715715. When the user enters his/her mobile number initially validation should be performed that the first value entered by user is '+' and then the number that is entered after + should not be less that 0.
但在此之后我感到困惑的是如何获得在+和 - 之间输入的数字的数量,因为用户输入国家代码并且在+和 - 之间输入的数字的长度必须是动态的而不是静态的。 / p>
But after this I am getting confused that how to get the number of numbers entered between + and -, because user enters the country code and the length of numbers entered between + and - must be dynamic not static.
推荐答案
试试这个:
NSString *code=@"+91-99999999";
NSRange rr2 = [code rangeOfString:@"+"];
NSRange rr3 = [code rangeOfString:@"-"];
int lengt = rr3.location - rr2.location - rr2.length;
int location = rr2.location + rr2.length;
NSRange aa;
aa.location = location;
aa.length = lengt;
code = [code substringWithRange:aa];
NSLog(@"%@",code);
这篇关于如何在文本字段中对用户在iPhone中输入的电话号码进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!