ID card number verification-PHP source code

Source: Internet
Author: User
ID card number verification

Function idcard_check ($ id) {if (! Preg_match ('/^ \ d {18 }$ | ^ \ d {17} X $/', $ id) {return false ;} $ id = str_split ($ id); $ x = array (, 2); $ y = array, 'X', 9, 8, 7, 6, 5, 4, 3, 2); $ sum = 0; foreach ($ X as $ k => $ v) {$ sum + = $ id [$ k] * $ v;} return (string) $ y [$ sum % 11] ===$ id [count ($ id) -1];} var_dump (idcard_check ('If the end of the ID card number is X, it should be capitalized '));

2.Add a js version

Function idcard_check (id) {if (! /^ \ D {18 }$ | ^ \ d {17} X $ /. test (id) return false; var code = [, 'X',]; var sum = 0; for (var I = 17; i> 0; I --) {sum + = id [17-i] * Math. pow (2, I) % 11;} return id [17] = code [sum % 11];} console. log (idcard_check ('X at the end of the ID card number must be string type' in uppercase '));

The above is the content of ID card number verification. For more information, see PHP Chinese network (www.php1.cn )!

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.