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

Mar 30, 2018 pm 02:47 PM
Format ID card

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

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write the qq mailbox format? What is the qq mailbox format? How to write the qq mailbox format? What is the qq mailbox format? Feb 22, 2024 pm 03:40 PM

QQ email: QQ number@qq.com, English QQ email: English or numbers@qq.com, foxmail email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: QQ Mailbox 6.3.3 Analysis 1QQ mailbox has four formats, commonly used QQ mailbox: QQ number@qq.com, English QQ mailbox: English or numbers@qq.com, foxmail Email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Supplement: What is qq mailbox? 1 The earliest QQ mailbox was only between QQ users

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

In iOS 17 and macOS Sonoma, Apple has added new formatting options for Apple Notes, including block quotes and a new Monostyle style. Here's how to use them. With additional formatting options in Apple Notes, you can now add block quotes to your notes. The block quote format makes it easy to visually offset sections of writing using the quote bar to the left of the text. Just tap/click the "Aa" format button and select the block quote option before typing or when you are on the line you want to convert to a block quote. This option applies to all text types, style options, and lists, including checklists. In the same Format menu you can find the new Single Style option. This is a revision of the previous "equal-width"

What is the file format of ink? What is the file format of ink? Feb 03, 2023 pm 02:32 PM

ink is the file format of a shortcut; a shortcut is a file object that is similar in function to a symbolic link, but is essentially different from a symbolic link; a shortcut is an ordinary file, not a symbol, and its extension is ".lnk" , so the shortcut can be copied, moved, changed or deleted; the shortcut can point to a file, folder or any other legal location in the system.

One ID card can open several Taobao stores One ID card can open several Taobao stores Oct 10, 2023 pm 01:51 PM

An ID card can only open one Taobao store. Sellers should abide by the regulations of the Taobao platform and do not try to use other people's ID cards to open multiple stores. Sellers can expand their business and increase sales by merging stores and opening branches. However, it should be noted that each branch needs to use an independent ID card for real-name authentication.

What is the format of pptm? What is the format of pptm? Jan 11, 2021 pm 02:46 PM

pptm is a file format of PowerPoint in the office office suite. Its full name is "macro-enabled PowerPoint presentation". pptm files can only be opened with office software of version 2007 and above. If opened with other versions of software, problems such as inability to edit and incomplete images will occur.

What format is m3u8? What format is m3u8? Feb 20, 2023 pm 01:38 PM

m3u8 is UTF-8 encoding format. M3U8 refers to the UTF-8 encoded M3U file, and the M3U file records an index plain text file. When you open it, the playback software does not play it, but finds the network address of the corresponding audio and video file based on its index for online processing. Play. M3U8 is a common streaming media format that mainly exists in the form of a file list. It supports both live broadcast and on-demand playback. It is especially commonly used on platforms such as Android and iOS.

What format file is cr3? What format file is cr3? Jun 23, 2021 pm 03:22 PM

CR3 files are original raw files recorded by Canon's new cameras using the Digic8 image processor. cr3 files can be opened through Canon ddp, which is software for performing image adjustments such as "RAW development" on RAW images.

What format is rtf What format is rtf Jan 30, 2023 pm 02:35 PM

The full name of RTF is "Rich Text Format", which means "Rich Text Format" or "Multiple Text Format" in Chinese. It is a cross-platform document format developed by Microsoft; RTF is a document format that Word can save in order to be compatible with other word processing software. , is a file similar to DOC format (Word document) and has good compatibility. For ordinary users, the RTF format is a good file format conversion tool for transferring formatted text documents between different applications.

See all articles