Java code
Recently, I was working on a user management program. During the process, I checked the user registration information on the Internet for a long time. I didn't find the ready-made code, but I had to write it myself,
Recently, I was working on a user management program. During the process, I checked the user registration information on the Internet for a long time. I didn't find the ready-made code, but I had to write it myself. Java code
Put the code here and take it directly. Back up your own data for future use.
Put the code here and take it directly. Back up your own data for future use. Java code
// Verify the user name
+ (BOOL) validateUserName: (NSString *) str
{
NSString * patternStr = [NSString stringWithFormat: @ "^. {0, 4 }$ |. {21,} | ^ [^ A-Za-z0-9 \ u4E00-\ u9FA5] | [^ \ w \ u4E00-\ u9FA5. -] | ([_. -]) \ 1 "];
NSRegularExpression * regularexpression = [[NSRegularExpression alloc]
InitWithPattern: patternStr
Options: NSRegularExpressionCaseInsensitive
Error: nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString: str
Options: NSMatchingReportProgress
Range: NSMakeRange (0, str. length)];
[Regularexpression release];
If (numberofMatch> 0)
{
Return YES;
}
Return NO;
}
// Verify the User Password
+ (BOOL) validateUserPasswd: (NSString *) str
{
NSRegularExpression * regularexpression = [[NSRegularExpression alloc]
InitWithPattern: @ "^ [a-zA-Z0-9] {6, 16} $"
Options: NSRegularExpressionCaseInsensitive
Error: nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString: str
Options: NSMatchingReportProgress
Range: NSMakeRange (0, str. length)];
[Regularexpression release];
If (numberofMatch> 0)
{
NSLog (@ "% @ isNumbericString: YES", str );
Return YES;
}
NSLog (@ "% @ isNumbericString: NO", str );
Return NO;
}
// Verify the user's birthday
+ (BOOL) validateUserBornDate: (NSString *) str
{
NSString * patternStr = @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ ";
NSRegularExpression * regularexpression = [[NSRegularExpression alloc]
InitWithPattern: patternStr
Options: NSRegularExpressionCaseInsensitive
Error: nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString: str
Options: NSMatchingReportProgress
Range: NSMakeRange (0, str. length)];
[Regularexpression release];
If (numberofMatch> 0)
{
NSLog (@ "% @ isNumbericString: YES", str );
Return YES;
}
NSLog (@ "% @ isNumbericString: NO", str );
Return NO;
}
// Verify the user's mobile phone number
+ (BOOL) validateUserPhone: (NSString *) str
{
NSRegularExpression * regularexpression = [[NSRegularExpression alloc]
InitWithPattern: @ "(\ d {11}) | ^ (\ d {7, 8}) | (\ d {4} | \ d {3 }) -(\ d {7,8}) | (\ d {4} | \ d {3})-(\ d {7,8 }) -(\ d {4} | \ d {3} | \ d {2} | \ d {1}) | (\ d {7, 8 }) -(\ d {4} | \ d {3} | \ d {2} | \ d {1}) $ )"
Options: NSRegularExpressionCaseInsensitive
Error: nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString: str
Options: NSMatchingReportProgress
Range: NSMakeRange (0, str. length)];
[Regularexpression release];
If (numberofMatch> 0)
{
NSLog (@ "% @ isNumbericString: YES", str );
Return YES;
}
NSLog (@ "% @ isNumbericString: NO", str );
Return NO;
}
// Verify the user's email address
+ (BOOL) validateUserEmail: (NSString *) str
{
NSRegularExpression * regularexpression = [[NSRegularExpression alloc]
InitWithPattern: @ "\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\\. \ w + ([-.] \ w + )*"
Options: NSRegularExpressionCaseInsensitive
Error: nil];
NSUInteger numberofMatch = [regularexpression numberOfMatchesInString: str
Options: NSMatchingReportProgress
Range: NSMakeRange (0, str. length)];
[Regularexpression release];
If (numberofMatch> 0)
{
NSLog (@ "% @ isNumbericString: YES", str );
Return YES;
}
NSLog (@ "% @ isNumbericString: NO", str );
Return NO;
}
Author HOTFM