How does PHP verify whether the entered bank card is valid?-php Tutorial

Source: Internet
Author: User
PHP: how to verify whether the entered bank card is valid? I am currently making an error entry. when I enter a bank card, I want to verify whether the bank card is valid, it is like an ID card. if you can verify whether the character string meets the requirements, find the complete code! I heard that there is a luhn algorithm, but this seems incomplete. Who gave me a function PHP that can be used immediately? how can I verify whether the bank card I entered is valid?
How does PHP verify whether the entered bank card is valid? when I enter a bank card for a lost object, I want to verify whether the bank card is valid like an ID card, can verify whether the string matches, please find the complete code! I heard that there is a luhn algorithm, but this seems incomplete. Who will give me a function that can be used right away?

Php, code, verification, bank card

Share:


------ Solution --------------------
The LUHN algorithm is mainly used to calculate the validity of credit card and other document numbers.
1. starting from the last digit of the card number, multiply the even digit by 2. if the result is two digits, add and save the two digits.
2. add all numbers to get the sum.
3. if the credit card number is valid, the sum can be divided by 10.

Therefore
Function luhn ($ s ){
$ N = 0;
For ($ I = strlen ($ s)-1; $ I >=0; $ I --){
If ($ I % 2) $ n + = $ s {$ I };
Else {
$ T = $ s {$ I} * 2;
If ($ t> 9) $ t = $ t {0} + $ t {1 };
$ N + = $ t;
}
}
Return ($ n % 10) = 0;
}

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.