-2

Im stuck in validating cards. I have visa, american express, master card, JCB, union pay,sct and maestro. I have listed them in a select form. Now I want to validate when user select any of them , the input field should match with selected card according to their pattern. I have validate date but this card validation has giving me hard time since I could not find any good tutorial or code example.

Can anybody help with this, with code or example or links?

Sushan Shrestha
  • 21
  • 1
  • 1
  • 8
  • Simple searching turned up https://stackoverflow.com/q/36537846/62576 At least make an effort to search for existing questions and answers first before posting a new question. Also read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Ken White Jul 22 '17 at 04:08

1 Answers1

0

You can try this.

For Php:

    <?php

    $submitbutton= $_POST['submitbutton'];

    $number= $_POST['number_entered'];

    function validatecard($number)
     {
        global $type;

        $cardtype = array(
            "visa"       => "/^4[0-9]{12}(?:[0-9]{3})?$/",
            "mastercard" => "/^5[1-5][0-9]{14}$/",
            "amex"       => "/^3[47][0-9]{13}$/",
            "discover"   => "/^6(?:011|5[0-9]{2})[0-9]{12}$/",
        );

        if (preg_match($cardtype['visa'],$number))
        {
        $type= "visa";
            return 'visa';

        }
        else if (preg_match($cardtype['mastercard'],$number))
        {
        $type= "mastercard";
            return 'mastercard';
        }
        else if (preg_match($cardtype['amex'],$number))
        {
        $type= "amex";
            return 'amex';

        }
        else if (preg_match($cardtype['discover'],$number))
        {
        $type= "discover";
            return 'discover';
        }
        else
        {
            return false;
        } 
     }

validatecard($number);


?>

For JavaScript Follow this link:

Replace your variable when needed.

Happy Coding.

Gangani Roshan
  • 583
  • 1
  • 8
  • 25