PHP: verifying that the credit card number is correct function _ PHP Tutorial

Source: Internet
Author: User
PHP verifies whether the credit card number is correct. PHP verification of credit card number is correct function this article mainly introduces PHP verification of credit card number is correct function, this article directly gives the implementation code, for more information about how to use PHP to verify whether the credit card number is correct

This article mainly introduces PHP functions used to verify whether a credit card number is correct. This article provides the implementation code directly. For more information, see

You can use the following PHP function to verify whether a card number is a credit card:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Function validateCard ($ cardnumber)

{

$ Cardnumber = preg_replace ("/\ D | \ s/", "", $ cardnumber); # strip any non-digits

$ Cardlength = strlen ($ cardnumber );

If ($ cardlength! = 0)

{

$ Parity = $ cardlength % 2;

$ Sum = 0;

For ($ I = 0; $ I <$ cardlength; $ I ++)

{

$ Digit = $ cardnumber [$ I];

If ($ I % 2 = $ parity) $ digit = $ digit * 2;

If ($ digit> 9) $ digit = $ digit-9;

$ Sum = $ sum + $ digit;

}

$ Valid = ($ sum % 10 = 0 );

Return $ valid;

}

Return false;

}

Note <>: For more exciting tutorials, please pay attention to the help house programming

This article describes the PHP function used to verify whether a credit card number is correct. the implementation code is provided in this article...

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.