剛轉行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!