Please help to fix my jscript code for my modulus form validation of credit card number. when i key in the valid credit number the form validation will pass but if not it will refer to my documentbyelementid which is invalidcard_error. const checkingFactor = "432765432"; //to store the credit card number let creditCardNum = ""; //Loop till we get the valid 10 digit long credit card number while(creditCardNum.length != 10){ //If the credit card length is not 10 then ask the user for the credit card number again creditCardNum = prompt("Please enter your credit card number : ", "1234567890"); //Check if the credit card number is 10 digit long or not if(creditCardNum.length != 10){ alert("Invalid credit card number should be 10 digits long."); continue; } //Check if the credit card contains only numbers if(isNaN(creditCardNum)){ alert("Invalid credit card number. Should only contain digits"); continue; } } //TO store the sum of the credit card digits let cardDigitSum = 0; //Once we get the valid number from the above prompt Get the Multiply factor for(let i = 0 ; i< creditCardNum.length-1 ; i++){ //Add the sum in the card digit sum cardDigitSum += ( parseInt(creditCardNum[i]) * parseInt(checkingFactor[i]) ); console.log(cardDigitSum); } //Get the checkdigit . Checkdigit = 11 - (creditDigitSum % 11) let checkDigit = 11 - (cardDigitSum % 11); console.log(creditCardNum[creditCardNum.length-1]); //Compare the check digit with the last digit of the credit card number if(checkDigit == parseInt(creditCardNum[creditCardNum.length-1])){ alert("The number is the correct credit card number.") }else{ alert("The number is invalid credit card number."); }
Please help to fix my jscript code for my modulus form validation of credit card number.
when i key in the valid credit number the form validation will pass but if not it will refer to my documentbyelementid which is invalidcard_error.
const checkingFactor = "432765432";
//to store the credit card number
let creditCardNum = "";
//Loop till we get the valid 10 digit long credit card number
while(creditCardNum.length != 10){
//If the credit card length is not 10 then ask the user for the credit card number again
creditCardNum = prompt("Please enter your credit card number : ", "1234567890");
//Check if the credit card number is 10 digit long or not
if(creditCardNum.length != 10){
alert("Invalid credit card number should be 10 digits long.");
continue;
}
//Check if the credit card contains only numbers
if(isNaN(creditCardNum)){
alert("Invalid credit card number. Should only contain digits");
continue;
}
}
//TO store the sum of the credit card digits
let cardDigitSum = 0;
//Once we get the valid number from the above prompt Get the Multiply factor
for(let i = 0 ; i< creditCardNum.length-1 ; i++){
//Add the sum in the card digit sum
cardDigitSum += ( parseInt(creditCardNum[i]) * parseInt(checkingFactor[i]) );
console.log(cardDigitSum);
}
//Get the checkdigit . Checkdigit = 11 - (creditDigitSum % 11)
let checkDigit = 11 - (cardDigitSum % 11);
console.log(creditCardNum[creditCardNum.length-1]);
//Compare the check digit with the last digit of the credit card number
if(checkDigit == parseInt(creditCardNum[creditCardNum.length-1])){
alert("The number is the correct credit card number.")
}else{
alert("The number is invalid credit card number.");
}
Step by step
Solved in 3 steps with 6 images