search
HomeWeb Front-endJS TutorialHow to write the regular rules to determine the format of ID card and bank card number

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

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 <p style="text-align: left;">在接下来便是银行卡号的的判断返回YES或者是真是合法,反之不合法</p><pre class="brush:php;toolbar:false">#pragma mark 判断银行卡号是否合法
-(BOOL)isBankCard:(NSString *)cardNumber{
  if(cardNumber.length==0){
    return NO;
  }
  NSString *digitsOnly = @"";
  char c;
  for (int i = 0; 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;
}

相信看了本文案例你已经掌握了方法,更多精彩请关注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
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
如何在苹果笔记中使用块引号如何在苹果笔记中使用块引号Oct 12, 2023 pm 11:49 PM

在iOS17和macOSSonoma中,Apple为AppleNotes添加了新的格式选项,包括块引号和新的Monostyle样式。以下是使用它们的方法。借助AppleNotes中的其他格式选项,您现在可以在笔记中添加块引用。块引用格式可以轻松地使用文本左侧的引用栏直观地偏移部分的写作。只需点击/单击“Aa”格式按钮,然后在键入之前或当您在要转换为块引用的行上时选择块引用选项。该选项适用于所有文本类型、样式选项和列表,包括清单。在同一“格式”菜单中,您可以找到新的“单样式”选项。这是对先前“等宽

pptm是什么格式pptm是什么格式Jan 11, 2021 pm 02:46 PM

pptm是office办公套件中powerpoint的一种文件格式,全名是“启用宏的PowerPoint演示文稿”。pptm文件只能用2007及以上版本的office软件打开,如果用其他版本软件打开会出现无法编辑、图片不完整等问题。

一个身份证可以开几个淘宝店一个身份证可以开几个淘宝店Oct 10, 2023 pm 01:51 PM

一个身份证只能开设一个淘宝店,卖家应该遵守淘宝平台的规定,不要尝试使用其他人的身份证来开设多个店铺。卖家可以通过店铺合并和开设分店的方式来拓展业务,提高销售额。但是需要注意的是,每个分店都需要使用独立的身份证进行实名认证。

ink是什么文件格式ink是什么文件格式Feb 03, 2023 pm 02:32 PM

ink是快捷方式的文件格式;快捷方式是一种功能上类似符号链接的文件对象,但与符号链接有本质的不同;快捷方式是普通的文件,而非符号,其扩展名是“.lnk”,因此快捷方式可以被复制、移动、更改或删除;快捷方式可以指向文件、文件夹或其他任何系统中合法的位置。

m3u8是什么格式m3u8是什么格式Feb 20, 2023 pm 01:38 PM

m3u8是UTF-8编码格式。M3U8是指UTF-8编码的M3U文件,而M3U文件是记录了一个索引纯文本文件,打开它时播放软件并不是播放它,而是根据它的索引找到对应的音视频文件的网络地址进行在线播放。M3U8是一种常见的流媒体格式,主要以文件列表的形式存在,既支持直播又支持点播,尤其在Android、iOS等平台最为常用。

cr3是什么格式文件cr3是什么格式文件Jun 23, 2021 pm 03:22 PM

CR3文件是佳能采用Digic8图像处理器的新型相机记录的原始raw文件。cr3的文件可以通过佳能ddp打开,是对RAW图像进行“RAW显像”等图像调节的软件。

exb是什么格式文件exb是什么格式文件Mar 10, 2021 pm 03:02 PM

exb文件是CAXA制图软件所生成的专用格式,CAXA是中国最大的制造业软件提供商,exb文件就是CAXA二维绘图软件电子图板的数据文件。

rtf是什么格式rtf是什么格式Jan 30, 2023 pm 02:35 PM

rtf全称“Rich Text Format”,中文意思为“富文本格式”或“多文本格式”,是由微软公司开发的跨平台文档格式;RTF是word为了与其他字处理软件兼容而能够保存的文档格式,是一种类似DOC格式(Word文档)的文件,有很好的兼容性。对普通用户而言,RTF格式是一个很好的文件格式转换工具,用于在不同应用程序之间进行格式化文本文档的传送。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function