-1

Here is the fiddle. I am trying to make it so when you type in your credit card number, it can only be a 16 digit number.

Right now I am using:

var i = $('#input1').val();
var ii = $('#input2').val();
var iii = $('#input3').val();
var iiii = $('#input4').val();
if (i !== "" && ii !== "" && iii !== "" && iiii !== '') {

but this allows it to be letters or symbols or numbers.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Joe Pigott
  • 7,981
  • 6
  • 31
  • 43

5 Answers5

1

If you only care about validating if all are numbers, see this fiddle: http://jsfiddle.net/y7sf4/

It uses this function:

function IsNumeric(input)
{
    return (input - 0) == input && (input+'').replace(/^\s+|\s+$/g, "").length > 0;
}

But really you should try something that validates credit cards entirely, not just if it's a numeric 16 digit number.

Take a look at http://jquerycreditcardvalidator.com/

Tor
  • 784
  • 1
  • 13
  • 25
0

You can check each of the values with this function to ensure it is numeric, which is what I would probably do:

function isNumber(val) {
return !isNaN(val - 0) && val != null;
}

Or, if you wanted to get really fancy, you could always read up on regular expressions, and use javascripts regex functions... http://www.regular-expressions.info/creditcard.html

McSenstrum
  • 127
  • 6
0

You could use a single input, set the maxlenght, this will only allow 16 characters to be put into the textbox. Then just test the length and that all the characters are numbers with the javascript.

HTML

<input type="text" name="ccn" maxlength="16" id="ccn">

Javascript

var ccn = $('#ccn').val();
//Test the length, and use a regex to test 
//that there are 16 numbers
if( ccn.length < 16 || !/[0-9]{16}/.test(ccn) ) {
   //Invalid credit card number do error code here
}

or with staying with your grouped inputs set maxlength to 4 for each box and test each one for length and valid numbers

HTML

<input type="text" name="ccn1" maxlength="4" id="ccn1">
<input type="text" name="ccn2" maxlength="4" id="ccn2">
<input type="text" name="ccn3" maxlength="4" id="ccn3">
<input type="text" name="ccn4" maxlength="4" id="ccn4">

Javascript

function is_ccnGroup(group) {
    if( group.length < 4 || !/[0-9]{4}/.test(group) ) {
       return false;
    }

    return true;    
}

if( is_ccnGroup($('#ccn1').val()) &&
    is_ccnGroup($('#ccn2').val()) &&
    is_ccnGroup($('#ccn3').val()) &&
    is_ccnGroup($('#ccn4').val()) ) {
    // All the groups seem to be valid
} else {
   //One or more of the groups are invalid
}

Probably could also use jquery's .map function to cut down the code for the 4 groups way.

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
  • You can improve that HTML by adding a `pattern` attribute to accommodate modern browsers. – rink.attendant.6 Aug 19 '13 at 16:58
  • Can you provide a fiddle? – Joe Pigott Aug 19 '13 at 17:22
  • I have copy and pasted the code and I don't see what is supposed to be happening. Can you explain? – Joe Pigott Aug 21 '13 at 01:13
  • forgot to add `!` infront of the regex check, and there was a syntax error, reedited answer to fix. [first fiddle](http://jsfiddle.net/yKnGk/),[second fiddle](http://jsfiddle.net/yKnGk/1/), in the first example `ccn.length < 16 || !/[0-9]{16}/.test(ccn)` means if the input is less then 16 characters or not all the characters are numbers, in the second example it grabs each of the four boxes inputs and checks if all 4 are 4 characters in length and are numbers – Patrick Evans Aug 21 '13 at 09:48
0

You can do this in HTML(5), its as simple as:

<input type='number' pattern='.{16}' />

Note that not all browsers support type='number' or pattern though I believe the recent versions of all the major browsers do.

Check it out over at jsfiddle

Muleskinner
  • 14,150
  • 19
  • 58
  • 79
0

Use the JQuery Mask Plugin... Then in your code you can set all fields to accept only numbers with this function:

$(document).ready(function () {

    ...
    $("#input4").mask("9999 9999 9999 9999");
});

jsfiddle here

bastos.sergio
  • 6,684
  • 4
  • 26
  • 36