Common regular Expression identity card information and the judgment of bank card information

Source: Internet
Author: User
Tags modulus

Mailbox
+ (BOOL) Validateemail: (NSString *) email
{
NSString *emailregex = @ "[A-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\\. [A-za-z] {2,4} ";
Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Emailregex];
return [Emailtest Evaluatewithobject:email];
}

Mobile phone number verification. Yes, the phone number.
+ (BOOL) Validatemobile: (NSString *) mobile
{
Phone number starts with 13, 14, 15,17,18, eight \d numeric characters
NSString *phoneregex = @ "(^1[3|4|5|7|8][0-9]{9}$)";
Nspredicate *phonetest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Phoneregex];
return [Phonetest Evaluatewithobject:mobile];
}

User name to determine whether to include Chinese characters
+ (BOOL) Validateusername: (NSString *) name{
for (int i=0; i< [name length];i++) {
int a = [name characteratindex:i];
if (A > 0x4e00 && a < 0X9FFF) {
return YES;
}
} return NO;

}

ID number
+ (BOOL) Validateidentitycard: (NSString *) identitycard{

BOOL Flag;
if (identitycard.length <= 0)
{
flag = NO;
return flag;
}

NSString *regex2 = @ "^ (^[1-9]\\d{7} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) \\d{3}$) | (^[1-9]\\d{5}[1-9]\\d{3} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) ((\\d{4}) |\\d{3}[xx]) $) $ ";
Nspredicate *identitycardpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Regex2];
Flag = [Identitycardpredicate Evaluatewithobject:identitycard];


If you pass this verification, the ID card is in the correct format, but the accuracy needs to be calculated
if (flag)
{
if (identitycard.length==18)
{
Save the first 17-bit weighting factor in an array
Nsarray * Idcardwiarray = @[@ "7", @ "9", @ "10", @ "5", @ "8", @ "4", @ "2", @ "1", @ "6", @ "3", @ "7", @ "9", @ "10", @ "5", @ "8", @ " 4 "@" 2 "];

This is the 11-bit remainder, the verification code, which is divided by 11, which can also be saved as an array
Nsarray * Idcardyarray = @[@ "1", @ "0", @ "10", @ "9", @ "8", @ "7", @ "6", @ "5", @ "4", @ "3", @ "2"];

Used to hold the sum of the first 17 digits after the weighted factor.

Nsinteger idcardwisum = 0;
for (int i = 0;i < 17;i++)
{
Nsinteger Substrindex = [[Identitycard substringwithrange:nsmakerange (I, 1)] integervalue];
Nsinteger Idcardwiindex = [[Idcardwiarray objectatindex:i] integervalue];

idcardwisum+= Substrindex * IDCARDWIINDEX;

}

Calculates the position of the array where the checksum is located
Nsinteger idcardmod=idcardwisum%11;

Get the last ID number.
NSString * idcardlast= [Identitycard substringwithrange:nsmakerange (17, 1)];

If it equals 2, then the check code is 10, the last digit of the ID number should be X
if (idcardmod==2)
{
if ([Idcardlast isequaltostring:@ "X"]| | [Idcardlast isequaltostring:@ "X"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
Use the calculated verification code to match the last identity card number, if the same, the explanation passed, otherwise it is invalid ID number
if ([Idcardlast isequaltostring: [Idcardyarray Objectatindex:idcardmod]])
{
return flag;
}
Else
{
flag = NO;
return flag;
}
}
}
Else
{
flag = NO;
return flag;
}
}
Else
{
return flag;
}
}

Bank card number
+ (BOOL) Validatebankcard: (NSString *) Bankcard
{
if (Bankcard.length = = 0) {
return NO;
}
NSString *digitsonly = @ "";
char c;
for (int i = 0; i < bankcard.length; i++)
{
c = [Bankcard characteratindex:i];
if (IsDigit (c))
{
Digitsonly =[digitsonly stringbyappendingformat:@ "%c", c];
}
}
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timestwo = false;
for (Nsinteger i = digitsonly.length-1; I >= 0; i--)
{
digit = [digitsonly characteratindex:i]-' 0 ';
if (timestwo)
{
Addend = digit * 2;
if (Addend > 9) {
Addend-= 9;
}
}
else {
Addend = digit;
}
sum + = Addend;
Timestwo =!timestwo;
}
int modulus = sum% 10;
return modulus = = 0;
}
Get Phone IP
+ (NSString *) Getphoneip {
NSString *address = @ "error";
struct Ifaddrs *interfaces = NULL;
struct Ifaddrs *temp_addr = NULL;
int success = 0;
Retrieve the current Interfaces-returns 0 on success
Success = Getifaddrs (&interfaces);
if (success = = 0) {
Loop through linked list of interfaces
TEMP_ADDR = interfaces;
while (temp_addr! = NULL) {
if (temp_addr->ifa_addr->sa_family = = af_inet) {
Check if interface is En0 which are the WiFi connection on the IPhone
if ([[[NSString Stringwithutf8string:temp_addr->ifa_name] isequaltostring:@ "En0"]) {
Get NSString from C String
address = [NSString stringwithutf8string:inet_ntoa ((struct sockaddr_in *) temp_addr->ifa_addr->sin_addr)];
}
}
TEMP_ADDR = temp_addr->ifa_next;
}
}
Free memory
Freeifaddrs (interfaces);
if ([Address isequaltostring:@ "error"])
{
Address = @ "192.168.1.1";
}
return address;

}

First time in App
+ (BOOL) Isfirsttimeenterapp
{
static NSString *fristtimeenterappkey = @ "Fristtimeenterappkey";
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
if ([Userdefaults Objectforkey:fristtimeenterappkey])
{
return (NO);
}
[Userdefaults setvalue:@ "" Forkey:fristtimeenterappkey];
return (YES);
}

/**
* MD5 Encrypted string
*/
+ (NSString *) md5nsstring: (NSString *) string{

if (string = = Nil | | [string length] = = 0) {
return nil;
}
unsigned char digest[cc_md5_digest_length], I;
CC_MD5 ([String utf8string], (int) [string lengthofbytesusingencoding:nsutf8stringencoding], digest);
nsmutablestring *ms = [nsmutablestring string];

for (i = 0; i < cc_md5_digest_length; i++) {
[Ms Appendformat:@ "%02X", (int) (digest[i])];
}

return [MS Copy];

}

Common regular Expression identity card information and the judgment of bank card information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.