iOS開(kāi)發(fā)中郵箱,電話號(hào)碼,身份證,密碼,昵稱(chēng)正則表達(dá)式驗(yàn)證

 //郵箱
 + (BOOL) validateEmail:(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];
 }  
  //手機(jī)號(hào)碼驗(yàn)證
  + (BOOL) validateMobile:(NSString *)mobile{   
  //手機(jī)號(hào)以13, 15,18開(kāi)頭,八個(gè) \d 數(shù)字字符   
   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];
 }
 //車(chē)牌號(hào)驗(yàn)證
 + (BOOL) validateCarNo:(NSString *)carNo{    
  NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";   
  NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];   
  NSLog(@"carTest is %@",carTest);    
  return [carTest evaluateWithObject:carNo];
}  
//車(chē)型+ (BOOL) validateCarType:(NSString *)CarType{   
 NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";  
 NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];   
 return [carTest evaluateWithObject:CarType];
}  
//用戶(hù)名+ (BOOL) validateUserName:(NSString *)name{    
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";    
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];   
BOOL B = [userNamePredicate evaluateWithObject:name];    return B;
}  
//密碼
+ (BOOL) validatePassword:(NSString *)passWord{    
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";   
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];    
return [passWordPredicate evaluateWithObject:passWord];
} 
 //昵稱(chēng)
 + (BOOL) validateNickname:(NSString *)nickname{  
  NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";    
  NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];    
  return [passWordPredicate evaluateWithObject:nickname];
}  
//身份證號(hào)
+ (BOOL) validateIdentityCard: (NSString *)identityCard{    
   BOOL flag;    
   if (identityCard.length <= 0) {     
        flag = NO;        
        return flag;   
   }   
   NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";   
   NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];   
   return [identityCardPredicate evaluateWithObject:identityCard];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容