rohit rohit - 2 months ago 15
jQuery Question

Credit card numbers starts with 1 & 15 digits long

i have written following jquery/javascript function for validating a credit card which is 15 digit long & must start with digit1.
so, how would i validate that the number will start with digit 1.

if ($("#ddlCreditCardType" + i).val() == 'TP') {
if (!($("#txtCreditCardNo" + i).val().length == 15)) {
$("#err" + i).html('Credit Card Number should be 15 digit');
status = 0;
}
else {
$("#err" + i).html('');
status = 1;
}

Answer

You could take the first letter of the value and compare it with 1:

if ($("#ddlCreditCardType" + i).val() == 'TP') {
    var value = $("#txtCreditCardNo" + i).val();
    if (value.length !== 15 || value[0] !== '1') {
        $("#err" + i).html('Credit Card Number should be 15 digit and start with 1');
        status = 0;
    }
}