Home > Web Front-end > JS Tutorial > How to write the regular rules to determine the format of ID card and bank card number

How to write the regular rules to determine the format of ID card and bank card number

php中世界最好的语言
Release: 2018-03-30 14:47:20
Original
3232 people have browsed it

这次给大家带来判断身份证与银行卡号格式的正则怎么写,使用判断身份证与银行卡号格式正则的注意事项有哪些,下面就是实战案例,一起来看一下。

1.有时候我们会用到上传身份证号,或者银行卡号,这个时候就需要我们对身份证号以及银行卡号,进行基本的判断。

下面便是身份证号的判断返回YES是合法,反之不合法

#pragma mark 判断身份证号是否合法
- (BOOL)judgeIdentityStringValid:(NSString *)identityString {
  if (identityString.length != 18) return NO;
  // 正则表达式判断基本 身份证号是否满足格式
  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 *identityStringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
  //如果通过该验证,说明身份证格式正确,但准确性还需计算
  if(![identityStringPredicate evaluateWithObject:identityString]) return NO;
  //** 开始进行校验 *//
  //将前17位加权因子保存在数组里
  NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
  //这是除以11后,可能产生的11位余数、验证码,也保存成数组
  NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
  //用来保存前17位各自乖以加权因子后的总和
  NSInteger idCardWiSum = 0;
  for(int i = 0;i < 17;i++) {
    NSInteger subStrIndex  = [[identityString substringWithRange:NSMakeRange(i, 1)] integerValue];
    NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
    idCardWiSum      += subStrIndex * idCardWiIndex;
  }
  //计算出校验码所在数组的位置
  NSInteger idCardMod=idCardWiSum%11;
  //得到最后一位身份证号码
  NSString *idCardLast= [identityString substringWithRange:NSMakeRange(17, 1)];
  //如果等于2,则说明校验码是10,身份证号码最后一位应该是X
  if(idCardMod==2) {
    if(![idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]) {
      return NO;
    }
  }else{
    //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
    if(![idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]) {
      return NO;
    }
  }
  return YES;
}
Copy after login

在接下来便是银行卡号的的判断返回YES或者是真是合法,反之不合法

#pragma mark 判断银行卡号是否合法
-(BOOL)isBankCard:(NSString *)cardNumber{
  if(cardNumber.length==0){
    return NO;
  }
  NSString *digitsOnly = @"";
  char c;
  for (int i = 0; i < cardNumber.length; i++){
    c = [cardNumber 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;
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

正则的数字验证详解(附代码)

JS正则表达式验证数字的实战归纳

The above is the detailed content of How to write the regular rules to determine the format of ID card and bank card number. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template