Validate form if a condition is met

javascript php jquery

68 просмотра

1 ответ

I am working on a credit card project. And I have a function in javascript (validateCreditCard) that helps me validate the credit card and return the credit card type. After getting the credit card type, I store as value of the hidden input cardType to be used after submission of the form. after clicking on the submit button, the user is taken to 'proceed.php' if the function cardFormValidate returns true.

This is what I have being trying to realize by writing those lines of code but the value of the hidden input is still empty. Kindly help me. Sorry I am not a native english speaker

function cardFormValidate() {

        //Card validation
        var card_number = $('#CreditCardNumber');
        card_number.validateCreditCard(function (result) {
            var cardType = (result.card_type == null) ? '' : result.card_type.name;
            if (result.valid) {
                $("#cardType").val(cardType);
                cardValid = true;
            } else {
                $("#cardType").val('');
                cardValid = false;
            }
          return cardValid;
        });

    }

    $(document).ready(function() {

        //Submit card form
        $("#PayButton").on('click',function(){
            if (cardFormValidate()) {
                //Move to proceed.php to treat the form
            }else{
              alert('bad credit card');
            }
        });
    });
<form method="post" action="proceed.php">
    <input name="cardType" type="hidden" id="cardType">
    <div class="form-group">
        <label for="NameOnCard">Name on card</label>
        <label for="NameOnCard"></label><input id="NameOnCard" class="form-control" type="text" name="NameOnCard" maxlength="255"/>
    </div>
    <div class="form-group">
        <label for="CreditCardNumber">Card number</label>
        <input id="CreditCardNumber" class="null card-image form-control" name="CreditCardNumber" type="text"/>
    </div>
    <button id="PayButton" type="submit"></button>
</form>

Автор: Prince Источник Размещён: 08.11.2019 10:55

Ответы (1)


1 плюс

Решение

I think validateCreditCard function is not executing properly and you are getting null value ..

1 ) first check Are You getting Card_Number??

put alert(card_number) if not then try this line

var card_number = $('#CreditCardNumber').val();

2) return data of validateCreditCard must have name property check it out otherwise you will get null value..

Автор: Dharti Sojitra Размещён: 20.08.2016 05:35
Вопросы из категории :
32x32