正則表達(dá)式相關(guān)(身份證、手機(jī)號(hào)等等)

開(kāi)發(fā)中有時(shí)候我們會(huì)遇到驗(yàn)證用戶的各種信息格式是否正確,這個(gè)時(shí)候我們客戶端就要使用正則表達(dá)式來(lái)驗(yàn)證。

郵箱驗(yàn)證:

- (BOOL)isValidEmail;

- (BOOL)isValidEmail

{

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:self];

}

手機(jī)號(hào)驗(yàn)證:(手機(jī)號(hào)碼驗(yàn)證可以再次細(xì)分為移動(dòng)聯(lián)通電信,再此則不再累述)

- (BOOL)isValidPhoneNum

{

//手機(jī)號(hào)以13, 15,18開(kāi)頭,八個(gè) \d 數(shù)字字符

NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:self];

}

車牌號(hào)驗(yàn)證:

- (BOOL)isValidCarNo

{

NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";

NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];

return [carTest evaluateWithObject:self];

}

網(wǎng)址驗(yàn)證:

- (BOOL)isValidUrl

{

NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";

return [self isValidateWithRegex:regex];

}

郵政編碼:

- (BOOL)isValidPostalcode {

NSString *phoneRegex = @"^[0-8]\\d{5}(?!\\d)$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:self];

}

純漢字:

- (BOOL)isValidChinese;

{

NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:self];

}

身份證驗(yàn)證:

- (BOOL)isValidIdCardNum

{

NSString *value = [self copy];

value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];

value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

int length = 0;

if (!value) {

return NO;

}else {

length = (int)value.length;

if (length != 15 && length !=18) {

return NO;

}

}

// 省份代碼

NSArray *areasArray =@[@"11", @"12", @"13", @"14", @"15", @"21", @"22", @"23", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"41", @"42", @"43", @"44", @"45", @"46", @"50", @"51", @"52", @"53", @"54", @"61", @"62", @"63", @"64", @"65", @"71", @"81", @"82", @"91"];

NSString *valueStart2 = [value substringToIndex:2];

BOOL areaFlag = NO;

for (NSString *areaCode in areasArray) {

if ([areaCode isEqualToString:valueStart2]) {

areaFlag = YES;

break;

}

}

if (!areaFlag) {

return NO;

}

NSRegularExpression *regularExpression;

NSUInteger numberofMatch;

int year = 0;

switch (length) {

case 15:

year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;

if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {

regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"? ? ? ? ? ? ? ? ? options:NSRegularExpressionCaseInsensitive error:nil];// 測(cè)試出生日期的合法性

}else {

regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"? ? ? ? ? options:NSRegularExpressionCaseInsensitive error:nil];// 測(cè)試出生日期的合法性

}

numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];

if(numberofMatch > 0) {

return YES;

}else {

return NO;

}

case 18:

year = [value substringWithRange:NSMakeRange(6,4)].intValue;

if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {

regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"options:NSRegularExpressionCaseInsensitive error:nil];// 測(cè)試出生日期的合法性

}else {

regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"

options:NSRegularExpressionCaseInsensitive error:nil];// 測(cè)試出生日期的合法性

}

numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];

if(numberofMatch > 0) {

int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;

int Y = S % 11;

NSString *M = @"F";

NSString *JYM = @"10X98765432";

M = [JYM substringWithRange:NSMakeRange(Y,1)]; // 判斷校驗(yàn)位

if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {

return YES;// 檢測(cè)ID的校驗(yàn)位

}else {

return NO;

}

}else {

return NO;

}

default:

return NO;

}

return NO;

}

去掉兩端空格和換行符:

- (NSString *)stringByTrimmingBlank

{

return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

}

去掉html格式:

- (NSString *)removeHtmlFormat;

{

NSString *str = [NSString stringWithFormat:@"%@", self];

NSError *error;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];

if (!error) {

str = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"$2$1"];

} else {

NSLog(@"%@", error);

}

NSArray *html_code = @[

@"\"", @"'", @"&", @"<", @">",

@"", @"?", @"¢", @"£", @"¤",

@"¥", @"|", @"§", @"¨", @"?",

@"a", @"?", @"?", @"", @"?",

@"ˉ", @"°", @"±", @"2", @"3",

@"′", @"μ", @"?", @"·", @"?",

@"1", @"o", @"?", @"?", @"?",

@"?", @"?", @"×", @"÷", @"à",

@"á", @"?", @"?", @"?", @"?",

@"?", @"?", @"è", @"é", @"ê",

@"?", @"ì", @"í", @"?", @"?",

@"D", @"?", @"ò", @"ó", @"?",

@"?", @"?", @"?", @"ù", @"ú",

@"?", @"ü", @"Y", @"T", @"?",

@"à", @"á", @"a", @"?", @"?",

@"?", @"?", @"?", @"è", @"é",

@"ê", @"?", @"ì", @"í", @"?",

@"?", @"e", @"?", @"ò", @"ó",

@"?", @"?", @"?", @"?", @"ù",

@"ú", @"?", @"ü", @"y", @"t",

@"?", @"?", @"?", @"?", @"?",

@"?", @"∈", @"?", @"?", @"∏",

@"∑", @"?", @"?", @"√", @"∝",

@"∞", @"∠", @"∧", @"∨", @"∩",

@"∪", @"∫", @"∴", @"~", @"?",

@"≈", @"≠", @"≡", @"≤", @"≥",

@"?", @"?", @"?", @"?", @"?",

@"⊕", @"?", @"⊥", @"?", @"Α",

@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",

@"Η", @"Θ", @"Ι", @"Κ", @"Λ",

@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",

@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",

@"Χ", @"Ψ", @"Ω", @"α", @"β",

@"γ", @"δ", @"ε", @"ζ", @"η",

@"θ", @"ι", @"κ", @"λ", @"μ",

@"ν", @"ξ", @"ο", @"π", @"ρ",

@"?", @"σ", @"τ", @"υ", @"φ",

@"χ", @"ψ", @"ω", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?", @"?", @"?", @"?", @"",

@"", @"", @"", @"", @"",

@"", @"–", @"—", @"‘", @"’",

@"?", @"“", @"”", @"?", @"?",

@"?", @"?", @"…", @"‰", @"′",

@"″", @"?", @"?", @" ̄", @"€",

@"?", @"←", @"↑", @"→", @"↓",

@"?", @"?", @"?", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?",

];

NSArray *code = @[

@""", @"'", @"&", @"<", @">",

@"?", @"?", @"¢", @"£", @"¤",

@"¥", @"|", @"§", @"¨", @"?",

@"a", @"?", @"?", @"-", @"?",

@"ˉ", @"°", @"±", @"2", @"3",

@"′", @"μ", @"?", @"·", @"?",

@"1", @"o", @"?", @"?", @"?",

@"?", @"?", @"×", @"÷", @"à",

@"á", @"?", @"?", @"?", @"?",

@"?", @"?", @"è", @"é", @"ê",

@"?", @"ì", @"í", @"?", @"?",

@"D", @"?", @"ò", @"ó", @"?",

@"?", @"?", @"?", @"ù", @"ú",

@"?", @"ü", @"Y", @"T", @"?",

@"à", @"á", @"a", @"?", @"?",

@"?", @"?", @"?", @"è", @"é",

@"ê", @"?", @"ì", @"í", @"?",

@"?", @"e", @"?", @"ò", @"ó",

@"?", @"?", @"?", @"?", @"ù",

@"ú", @"?", @"ü", @"y", @"t",

@"?", @"?", @"?", @"&exists;", @"?",

@"?", @"∈", @"?", @"?", @"∏",

@"∑", @"?", @"?", @"√", @"∝",

@"∞", @"∠", @"∧", @"∨", @"∩",

@"∪", @"∫", @"∴", @"~", @"?",

@"≈", @"≠", @"≡", @"≤", @"≥",

@"?", @"?", @"?", @"?", @"?",

@"⊕", @"?", @"⊥", @"?", @"Α",

@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",

@"Η", @"Θ", @"Ι", @"Κ", @"Λ",

@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",

@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",

@"Χ", @"Ψ", @"Ω", @"α", @"β",

@"γ", @"δ", @"ε", @"ζ", @"η",

@"θ", @"ι", @"κ", @"λ", @"μ",

@"ν", @"ξ", @"ο", @"π", @"ρ",

@"?", @"σ", @"τ", @"υ", @"φ",

@"χ", @"ψ", @"ω", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?", @"–", @"—", @"‘", @"’",

@"?", @"“", @"”", @"?", @"?",

@"?", @"?", @"…", @"‰", @"′",

@"″", @"?", @"?", @" ̄", @"€",

@"?", @"←", @"↑", @"→", @"↓",

@"?", @"?", @"?", @"?", @"?",

@"?", @"?", @"?", @"?", @"?",

@"?",

];

NSInteger idx = 0;

for (NSString *obj in code) {

str = [str stringByReplacingOccurrencesOfString:(NSString *)obj withString:html_code[idx]];

idx++;

}

return str;

}

工商稅號(hào):

- (BOOL)isValidTaxNo

{

NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:self];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容