刚转行iOS的搬砖工人,在此记录下这条路上的点点滴滴,共勉
废话:
现在APP的登录注册基本上已经离不开手机和邮箱绑定。今天在遇到了对输入的文本进行邮箱格式判断这个问题,所以google了一下,发现一个叫正则表达式的东西(应该大学学过,但是毫无印象)。
学习笔记:判断输入的手机、邮箱格式是否正确:
判断手机号码格式是否合法的正则表达式:
//手机号码的正则表达式
- (BOOL)isValidateMobile:(NSString *)mobile{
//手机号以13、15、18开头,八个\d数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
判断邮箱格式是否合法的正则表达式:
//邮箱地址的正则表达式
- (BOOL)isValidateEmail:(NSString *)email{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
新建一个工程,来实际测试一下,在storyboard放入相应的控件,关联到代码中:
控件关联到代码中完整代码:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *mobileText;//手机号码
@property (strong, nonatomic) IBOutlet UITextField *emailText;//邮箱地址
- (IBAction)mobileTestButton:(id)sender;//验证手机
- (IBAction)emailTestButon:(id)sender;//验证邮箱
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//手机号码的正则表达式
- (BOOL)isValidateMobile:(NSString *)mobile{
//手机号以13、15、18开头,八个\d数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//邮箱地址的正则表达式
- (BOOL)isValidateEmail:(NSString *)email{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
//点击按钮,弹出提示信息,验证输入的手机格式是否正确
- (IBAction)mobileTestButton:(id)sender {
if ([self isValidateMobile:_mobileText.text] == YES ) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"眼神不错,你输入的手机号有效"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ensureAction = [UIAlertAction actionWithTitle:@"原来如此"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:ensureAction];
[self presentViewController:alert animated:YES completion:nil];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"逗我呢?你这根本不是手机号啊"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"原来如此"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}
//点击按钮,弹出提示信息,验证输入的邮箱格式是否正确
- (IBAction)emailTestButon:(id)sender {
if ([self isValidateEmail:_emailText.text] ==YES) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"眼神不错,你输入的邮箱有效"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"原来如此"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"逗我呢?你这根本不是邮箱好吧"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action = [UIAlertAction actionWithTitle:@"原来如此"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}
@end
测试,输入一个无效的手机号码:
错误的手机号码
测试,输入一个正确的邮箱:
正确的邮箱地址
PS:眼睛很酸涩,本来想早点睡觉的。。。但是强大的毅力还是支撑着我又坚持着写了下来,虽然写得很草率。。。
睡觉!睡觉!今天终于可以在一点钟之前睡个觉了,简直nice!
网友评论
详细你可以看看这篇文章:http://www.superqq.com/blog/2015/06/27/yong-zheng-ze-biao-da-shi-yan-zheng-you-xiang-he-shou-ji-hao/
希望对你有用哦!谢谢支持!