Generally (in India) there are six digits in a cheque number (If some bank has different number pattern then I don't know). I don't know whether any other restriction on digits or not, like the first digit may be 'zero' or not. Which regEx or other method, I should use to verify that the cheque number entered in HTML form is valid or not. I don't know regEx well. It doesn't give false for a cheque number starting from 0.
I might go with one of the following, but which is the best ?
var chequeNos1 = ['111101', '222212', '234330', '044400'];
var chequeNos2 = ['111101', '222212', '234330', '444400'];
var chequeNos3 = ['111101', '222212', '234330', '44400'];
console.log(validateChequeNumbers(chequeNos1))
console.log(validateChequeNumbersByRegEx(chequeNos1))
console.log(validateChequeNumbers(chequeNos2))
console.log(validateChequeNumbersByRegEx(chequeNos2))
console.log(validateChequeNumbers(chequeNos3))
console.log(validateChequeNumbersByRegEx(chequeNos3))
function validateChequeNumbers(chequeNos){
var areAllChequeNosValid = chequeNos.every(chequeNo =>{
// console.log(100000 <= parseInt(chequeNo) && parseInt(chequeNo) <= 999999);
return 100000 <= parseInt(chequeNo) && parseInt(chequeNo) <= 999999;
})
return areAllChequeNosValid;
}
function validateChequeNumbersByRegEx(chequeNos){
var areAllChequeNosValid = chequeNos.every(chequeNo =>{
var validateReg = /^\d{6}$/;
// console.log(chequeNo.match(validateReg));
if(chequeNo.match(validateReg)){
return true;
}else{
return false;
}
})
return areAllChequeNosValid;
}
I'd encourage you to use the regex approach and also to factor out the validation test from the document parsing. That way you can create unit tests that check the validation logic independently.
Something like:
const chequeNumbers = [
'012345',
'123456',
'234567',
'345678'
];
const chequeNumberRE = /^\d{6}$/;
const validateChequeNumbers = checkNumbers => (
chequeNumbers.every(chequeNumber => chequeNumberRE.test(chequeNumber))
)
validateChequeNumbers(chequeNumbers)
Then call your function something like, validateChequeNumbersFromDocument or suchlike.
Please note: Indian cheque numbers may begin with any digit, not just zero.