前提
我們在平常注冊的時候都會校驗省份證辣往,但是很少會遇到校驗其他證件類型的兔院,今天正好結(jié)合ios調(diào)用js方法實現(xiàn)這一操作
實現(xiàn)方法
創(chuàng)建html文件
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> </head> <body> <input type="button" value="btn" id="btn" onclick="isjunguanzheng()" /> <input type="button" value="btn" id="btn" onclick="isHkongMacao()" /> <input type="button" value="btn" id="btn" onclick="isTaiw()" /> <input type="button" value="btn" id="btn" onclick="issfz()" /> <input type="button" value="btn" id="btn" onclick="isPassport()" /> <script> // 驗證身份證號碼有效性的方法 function issfz(value) { card_number = value.toLowerCase(); var aCity = {11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "內(nèi)蒙古", 21: "遼寧", 22: "吉林", 23: "黑龍江", 31: "上海", 32: "江蘇", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山東", 41: "河南", 42: "湖北", 43: "湖南", 44: "廣東", 45: "廣西", 46: "海南", 50: "重慶", 51: "四川", 52: "貴州", 53: "云南", 54: "西藏", 61: "陜西", 62: "甘肅", 63: "青海", 64: "寧夏", 65: "新疆", 71: "臺灣", 81: "香港", 82: "澳門", 91: "國外"}; if (!/^\d{17}(\d|x)$/i.test(card_number)) { //身份證不能為空 return false; } else { card_number = card_number.replace(/x$/i, "a"); if (aCity[parseInt(card_number.substr(0, 2))] == null) { //你的身份證地區(qū)非法 return false; } else { var sBirthday = card_number.substr(6, 4) + "-" + Number(card_number.substr(10, 2)) + "-" + Number(card_number.substr(12, 2)); var d = new Date(sBirthday.replace(/-/g, "/")); if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) { //身份證上的出生日期非法 return false; } else { var iSum = 0; for (var i = 17; i >= 0; i--) { iSum += (Math.pow(2, i) % 11) * parseInt(card_number.charAt(17 - i), 11); } if (iSum % 11 != 1) { //你輸入的身份證號非法 return false; } return true; //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女") } } } } //護(hù)照驗證 function isPassport(value) { var c = /\S{8,14}/; return c.test(value); } //軍官證驗證 function isjunguanzheng(value) { var reg = /\S{8,10}/; value = value.replace(/(^\s*)|(\s*$)/g, ""); if (reg.test(value) === false) { return false; } else { return true; } } //驗證港澳通行證 function isHkongMacao(value) { var a = /\S{9,12}/; return a.test(value); } //驗證臺胞證 function isTaiw(value, element) { var d = /\d{8,11}/; return d.test(value); } </script> </body> </html>
這里我們不需要了解太多 只需要知道 我們通過調(diào)用isjunguanzheng()、isHkongMacao()站削、isTaiw()坊萝、issfz()、isPassport()實現(xiàn)功能。
在原生ios項目中調(diào)用html文件中的js方法
將創(chuàng)建的html文件導(dǎo)入到我們的工程中十偶,在我們需要使用js功能的地方創(chuàng)建webView并調(diào)用
#import "ViewController.h" @interface ViewController () <UIWebViewDelegate> @property (nonatomic, strong) UIWebView* webview; @end @implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.webview = [[UIWebView alloc] init]; self.webview.backgroundColor = [UIColor clearColor]; self.webview.delegate = self; NSString* basePath = [[NSBundle mainBundle] bundlePath]; NSString* helpHtmlPath = [basePath stringByAppendingPathComponent:@"ver.html"]; NSURL* url = [NSURL fileURLWithPath:helpHtmlPath]; //加載本地html文件 [self.webview loadRequest:[NSURLRequest requestWithURL:url]]; [self.view addSubview:self.webview]; }- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event { NSString* str = [self.webview stringByEvaluatingJavaScriptFromString:@"issfz('130621199208225275');"]; NSLog(@"JS返回值:%@", str); } @end
通過該方式并能實現(xiàn)我們想要的功能菩鲜!