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.