在项目中输入经纬度的时候,老大要求用正则来验证输入内容,当我还在写正则的时候,他就在把他保存的一些正则发给了我们。我简单的做了一下处理,在这里贡献给大家,正则部分 Android
和iOS
通用的。
Android
的小伙伴直接参看RegularContansts.m
文件中的内容。
以下内容按照文件来写
NSString+Regular.h
#import <Foundation/Foundation.h>
@interface NSString (Regular)
/**
* 注册用户名有效性验证支持字母、数字、下划线、汉字
* (1).(6-32位)
* (2).(不能以 _开始或结束)
* (3).(必须包含一个字母或汉字)
*/
+ (BOOL)registerWithUserName:(NSString *)userName;
/**
* 登录用户名(包含用户名或手机)有效性验证支持字母、数字、下划线、汉字
* (1).(6-32位)
* (2).(不能以 _开始和结束)
*/
+ (BOOL)loginWithUserName:(NSString *)username;
/**
* 密码有效性验证支持字母、数字、下划线
* (1).(6-32位)
* (2).(不能以 _开始和结束)
* 注意:密码在前台做验证,后台得到的是一个32位md5之后的字符串
*/
+ (BOOL)verifyWithPassWord:(NSString *)password;
/**
* 手机号码验证
* (1).以13、14、15、17、18开头
* (2).11位数字
*/
+ (BOOL)verifyWithMobile:(NSString *)mobile;
/**
* qq号码
*/
+ (BOOL)verifyWithQQNumber:(NSString *)qqNumber;
/**
* 1w位的纯数字
*/
+ (BOOL)verifyWithNumbers:(NSString *)numbers;
/**
* ip正则
*/
+ (BOOL)verifyWithIP:(NSString *)ip;
/**
* 英文字母
*/
+ (BOOL)verifyWithEnglish:(NSString *)english;
/**
* 经度: -180.0~+180.0(整数部分为0~180,必须输入1到6位小数)
*/
+ (BOOL)verifyWithLongitude:(NSString *)lng;
/**
* 纬度: -90.0~+90.0(整数部分为0~90,必须输入1到6位小数)
*/
+ (BOOL)verifyWithLatitude:(NSString *)lat;
@end
NSString+Regular.m
#import "NSString+Regular.h"
#import "RegularContansts.h"
@implementation NSString (Regular)
+ (BOOL)judgeWithRegularString:(NSString *)reg value:(NSString *)value{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];
return [predicate evaluateWithObject:value];
}
+ (BOOL)registerWithUserName:(NSString *)userName{
return [NSString judgeWithRegularString:RegularVerifyRegisterName value:userName];
}
+ (BOOL)loginWithUserName:(NSString *)username{
return [NSString judgeWithRegularString:RegularVerifyLoginUserName value:username];
}
+ (BOOL)verifyWithPassWord:(NSString *)password{
return [NSString judgeWithRegularString:RegluarVerifyPassWord value:password];
}
+ (BOOL)verifyWithMobile:(NSString *)mobile{
return [NSString judgeWithRegularString:RegularVerifyMobileNumber value:mobile];
}
+ (BOOL)verifyWithQQNumber:(NSString *)qqNumber{
return [NSString judgeWithRegularString:RegularVerifyQQNumber value:qqNumber];
}
+ (BOOL)verifyWithNumbers:(NSString *)numbers{
return [NSString judgeWithRegularString:RegularVerifyNumbers value:numbers];
}
+ (BOOL)verifyWithIP:(NSString *)ip{
return [NSString judgeWithRegularString:RegularVerifyIpAddress value:ip];
}
+ (BOOL)verifyWithEnglish:(NSString *)english{
return [NSString judgeWithRegularString:RegularVerifyEnglish value:english];
}
+ (BOOL)verifyWithLongitude:(NSString *)lng{
return [NSString judgeWithRegularString:RegularVerifyLongitude value:lng];
}
+ (BOOL)verifyWithLatitude:(NSString *)lat{
return [NSString judgeWithRegularString:RegularVerifyLatitude value:lat];
}
@end
RegularContansts.h
#import <Foundation/Foundation.h>
@interface RegularContansts : NSObject
extern NSString * const RegularVerifyRegisterName;
extern NSString * const RegularVerifyLoginUserName;
extern NSString * const RegluarVerifyPassWord;
extern NSString * const RegularVerifyMobileNumber;
extern NSString * const RegularVerifyQQNumber;
extern NSString * const RegularVerifyNumbers;
extern NSString * const RegularVerifyIpAddress;
extern NSString * const RegularVerifyEnglish;
extern NSString * const RegularVerifyLongitude;
extern NSString * const RegularVerifyLatitude;
@end
RegularContansts.m
#import "RegularContansts.h"
@implementation RegularContansts
NSString * const RegularVerifyRegisterName = @"^(?!_)(?!.*?_$)(?=.*[a-zA-Z\u4e00-\u9fa5])[\\w\u4E00-\u9FA5\uF900-\uFA2D]{6,32}$";
NSString * const RegularVerifyLoginUserName = @"^(?!_)(?!.*?_$)[\\w\u4E00-\u9FA5\uF900-\uFA2D]{6,32}$";
NSString * const RegluarVerifyPassWord = @"^(?!_)(?!.*?_$)[\\w\u4E00-\u9FA5\uF900-\uFA2D]{6,32}$";
NSString * const RegularVerifyMobileNumber = @"^[1][3|4|5|7|8]\\d{9}$";
NSString * const RegularVerifyQQNumber = @"^\\d{5,10}$";
NSString * const RegularVerifyNumbers = @"^\\d{1,10000}$";
NSString * const RegularVerifyIpAddress = @"((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
NSString * const RegularVerifyEnglish = @"[a-zA-Z]";
NSString * const RegularVerifyLongitude = @"^[\\-\\+]?(0?\\d{1,2}\\.\\d{1,6}|1[0-7]?\\d{1}\\.\\d{1,6}|180\\.0{1,6})$";
NSString * const RegularVerifyLatitude = @"^[\\-\\+]?([0-8]?\\d{1}\\.\\d{1,6}|90\\.0{1,6})$";
@end
当然,你们也可以根据自己的细节需求来进行修改。如果能帮到各位,可不可以给个小心心呢?哈哈哈哈。。。。