任何人都有助于解决这个问题我是开发新手
我需要使用相同的文本字段登录电话号码以及电子邮件ID我怎么能实现这一点因此我必须设置电话号码的10位数限制以及美国像(838) - (838)-3333格式电话号码..和电子邮件ID与相同的文本提交..如何验证用户输入电话号码或电子邮件..任何人帮我解决问题 提前致谢 这是我的代码
if (!([self.usernameTextField.text validEmail] || [self.usernameTextField.text validateMobile])){
[self ShowAlert:@"Please enter a valid email address/phonenumber"];
}
- (BOOL)validEmail {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:self];
return isValid;
}
- (BOOL)validateMobile {
NSString *ACCEPTABLE_CHARECTERS = @" 0123456789+-";
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet];
NSString *filtered = [[self componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
// NSString* formattedNumber = [Contact formatPhoneNumber:phoneNumber codeLength:3 segmentLength:4];
return [self isEqualToString:filtered];
}
答案 0 :(得分:0)
在这里,我根据您的需要添加我的答案并自定义。
<强>步骤-1 强>
连接文本字段并在当前类上调用委托,例如
@interface ViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *currentTextfield;
@end
<强>步骤-2 强>
获取文本字段委托shouldChangeCharactersInRange
上的第一个字符,并检查它是否为数字(电话号码)或字符串(电子邮件)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)textEntered
{
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: textEntered];
NSString *abnRegex = @"[A-Za-z]+";
NSPredicate *abnTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", abnRegex];
if (([abnTest evaluateWithObject:resultingString] || resultingString.length == 0)) {
NSLog(@"its email");
}
else
{
NSLog(@"its phone number");
}
return YES;
}
<强>步骤-3 强>
如果是电话号码,则根据(838)-(838)-3333
格式
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)textEntered
{
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: textEntered];
NSString *abnRegex = @"[A-Za-z]+";
NSPredicate *abnTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", abnRegex];
if (([abnTest evaluateWithObject:resultingString] || resultingString.length == 0)) {
NSLog(@"its email");
}
else
{
NSLog(@"its phone number");
//(838)-(838)-3333
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
textField.text = [self formatPhoneNumber:resultingString deleteLastChar:YES];
} else {
textField.text = [self formatPhoneNumber:resultingString deleteLastChar:NO ];
}
return false;
}
return YES;
}
-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar {
if(simpleNumber.length==0) return @"";
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\s-\\(\\)]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];
// check if the number is to long
if(simpleNumber.length>10) {
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:10];
}
if(deleteLastChar) {
// should we delete the last digit?
simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1];
}
// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if(simpleNumber.length<7)
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d+)"
withString:@"($1)-($2)"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
else // else do this one..
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})(\\d+)"
withString:@"($1)-($2)-$3"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
return simpleNumber;
}
<强>步骤-4 强>
如果有效或无效,最后会在按钮操作上验证您的电子邮件或电话号码。
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
NSString *phoneRegex = @" (((\d{3}) ?)-((\d{3}) ?))?-\d{4}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
if([emailTest evaluateWithObject:currentTextfield.text] == NO && [phoneTest evaluateWithObject:currentTextfield.text] == NO)
{
NSLog(@"Please enter a valid email address/phonenumber");
}
else
{
//login successful
}
答案 1 :(得分:0)
使用此方法将对您有所帮助,并节省您的宝贵时间。
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:
(NSString *)string{
if (textField == _userIdTF){
NSString *totalString = [NSString stringWithFormat:@"%@%@",_userIdTF.text,string];
NSString *prefix;
if ([totalString length] == 1){
[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"checkValue"];
}
if ([totalString length] >= 1){
prefix = [totalString substringToIndex:1];
NSLog(@"first letter %@",prefix);
if ([prefix isEqualToString:@"1"] || [prefix isEqualToString:@"2"] || [prefix isEqualToString:@"2"] || [prefix isEqualToString:@"0"] || [prefix isEqualToString:@"3"]|| [prefix isEqualToString:@"4"] || [prefix isEqualToString:@"5"] || [prefix isEqualToString:@"6"] || [prefix isEqualToString:@"7"] || [prefix isEqualToString:@"8"] || [prefix isEqualToString:@"9"]){
[[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"checkValue"];
NSLog(@"checkvalu %@",[[NSUserDefaults standardUserDefaults]objectForKey:@"checkValue"]);
}else{
}
}
NSString *checkValue = [[NSUserDefaults standardUserDefaults]objectForKey:@"checkValue"];
NSLog(@"checkvalu %@",checkValue);
if ( [checkValue isEqual: @"1"] ) {
if ([totalString containsString:@"05"]) {
[totalString stringByReplacingOccurrencesOfString:totalString withString:@"+966"];
NSLog(@"phone number");
_userIdTF.text = [totalString stringByReplacingOccurrencesOfString:totalString withString:@"+966"];
}
}else{
prefix = totalString;
}
NSUInteger newLength = [textField.text length] + [string
length] - range.length;
if ([self.userIdTF.text containsString:@"+"]){
NSLog(@"number ppad");
NSString *acceptedCharsters = @"0123456789";
NSCharacterSet *cs = [[NSCharacterSet
characterSetWithCharactersInString:acceptedCharsters]
invertedSet];
NSString *filtered = [[string
componentsSeparatedByCharactersInSet:cs]
componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}else{
}
return (newLength > 50) ? NO : YES;
}
else{
return YES;
}
}