Credit Card Validation: You\'re starting your own credit card business. You\'ve
ID: 3717145 • Letter: C
Question
Credit Card Validation:
You're starting your own credit card business. You've come up with a new way to validate
credit cards with a simple function called validateCreditCard that returns true or false.
Here are the rules for a valid number:
Number must be 16 digits, all of them must be numbers
You must have at least two different digits represented (all of the digits cannot be the same)
The final digit must be even
The sum of all the digits must be greater than 16
The following credit card numbers are valid:
9999-9999-8888-0000
6666-6666-6666-1666
The following credit card numbers are invalid:
a923-3211-9c01-1112 invalid characters
4444-4444-4444-4444 only one type of number
1111-1111-1111-1110 sum less than 16
6666-6666-6666-6661 odd final number
Return an object indicating whether the credit card is valid,
and if not, what the error is{ valid: true, number: 'a923-3211-9c01-1112' }
{ valid: false, number: 'a923-3211-9c01-1112', error: ‘wrong_length
Write the above code in Javascript in HTML code
Explanation / Answer
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<label>Credit Card: </label>
<input type="text" id="txtCreditCard" placeholder="Enter Credit Card Number" />
<input type="button" id="btnValidate" value="Validate" />
<span id="spnError"></span>
</body>
</html>
<script type="text/javascript">
function validateCreditCard(OriginalCardNum) {
var ErrorMsg = "";
//remove the '-' charachters
var CardNum = OriginalCardNum.split('-').join('');
//error if length is not 16
if (CardNum.length != 16)
ErrorMsg = "wrong_length";
//error if final number is odd
else if (CardNum.charAt(15) % 2 != 0)
ErrorMsg = "odd_final_number";
else {
var sameDigits = 0, sumOfDigits = 0;
var firstChar = CardNum.charAt(0), curChar = ' ';
//loop through each cahrachter
for (var i = 0; i < CardNum.length; i++) {
curChar = CardNum.charAt(i);
//Error if non digit cahr is found
if (isNaN(curChar)) {
ErrorMsg = "invalid_characters";
break;
}
else //Sum of digits
sumOfDigits += parseInt(curChar, 10);
//count the same charachters
if (firstChar == curChar) {
sameDigits++;
}
}
//now check for errors
if (ErrorMsg == "") {
if (sumOfDigits <= 16) {
//less then 16, show error
ErrorMsg = "sum_less_than_16";
}
//same digits countis 16, show error
else if (sameDigits == 16)
ErrorMsg = "only_one_type_of_number";
}
}
// Define the object
var obj = {
valid: ErrorMsg == "" ? true : false,
number: OriginalCardNum,
error: ErrorMsg
};
return obj;
}
function validate() {
var object = validateCreditCard(document.getElementById("txtCreditCard").value.trim())
//set the error message to span spnError
document.getElementById("spnError").innerHTML = object.error;
//For modern browsers, if the above line doesn't supports
//document.getElementById("spnError").textContent=object.error;
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<!--<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>-->
</head>
<body>
<label>Credit Card: </label>
<input type="text" id="txtCreditCard" placeholder="Enter Credit Card Number" />
<input type="button" id="btnValidate" value="Validate" />
<span id="spnError"></span>
<script type="text/javascript">
function validateCreditCard(OriginalCardNum) {
var ErrorMsg = "";
//remove the '-' charachters
var CardNum = OriginalCardNum.split('-').join('');
//error if length is not 16
if (CardNum.length != 16)
ErrorMsg = "wrong_length";
//error if final number is odd
else if (CardNum.charAt(15) % 2 != 0)
ErrorMsg = "odd_final_number";
else {
var sameDigits = 0, sumOfDigits = 0;
var firstChar = CardNum.charAt(0), curChar = ' ';
//loop through each cahrachter
for (var i = 0; i < CardNum.length; i++) {
curChar = CardNum.charAt(i);
//Error if non digit cahr is found
if (isNaN(curChar)) {
ErrorMsg = "invalid_characters";
break;
}
else //Sum of digits
sumOfDigits += parseInt(curChar, 10);
//count the same charachters
if (firstChar == curChar) {
sameDigits++;
}
}
//now check for errors
if (ErrorMsg == "") {
if (sumOfDigits <= 16) {
//less then 16, show error
ErrorMsg = "sum_less_than_16";
}
//same digits countis 16, show error
else if (sameDigits == 16)
ErrorMsg = "only_one_type_of_number";
}
}
// Define the object
var obj = {
valid: ErrorMsg == "" ? true : false,
number: OriginalCardNum,
error: ErrorMsg
};
return obj;
}
function validate() {
var object = validateCreditCard(document.getElementById("txtCreditCard").value.trim())
//set the error message to span spnError
document.getElementById("spnError").innerHTML = object.error;
//For modern browsers, if the above line doesn't supports
//document.getElementById("spnError").textContent=object.error;
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.