I have a task where i need to use luhn algorithm to check credit cards are valid, and if they are the box goes green, if they are not the box goes red. I’ve found the below javascript on GitHub that looks to do what i need, but i am unsure on how to add the code to make the boxes change colour. I believe i need an if/ else statement but i’m not sure how to implement it. I was thinking something like this as the code for the colour change:
Hi I’m a fellow learner so i may not solve your problem but I’ll try to understand.
Basically the function cardnumber(value){} returns boolean either false or true.
Assuming that false means invalid CC and true means valid CC. Create a If-else statement like this: (after you state the function)
if(cardnumber(insertCreditCardNumberHere)){
/* do things when CC is valid */
document.getElementById(‘cardInput’).style.backgroundColor = “green”;
}
else{
/* do things when CC is invalid */
document.getElementById(‘cardInput’).style.backgroundColor = “red”;
}
Thank you, that’s the sort of method I had been trying but I couldn’t get it to work. The solution below seems to fix my problem but thank you for your help!