Random Credit Card Number

HTML code of Random Credit Card Number

<!DOCTYPE html>
<html>
<head>
<title>Random Credit Card Generator</title>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css”>
</head>
<body>
<div class=”container mt-5″>
<h1 class=”text-center mb-4″>Random Credit Card Generator</h1>
<form>
<div class=”form-group”>
<label for=”card-type”>Card Type:</label>
<select id=”card-type” class=”form-control”>
<option value=”visa”>Visa</option>
<option value=”mastercard”>Mastercard</option>
<option value=”amex”>American Express</option>
</select>
</div>
<button type=”button” class=”btn btn-primary” onclick=”generateCardNumber()”>Generate Card Number</button>
<div class=”form-group mt-4″>
<label for=”card-number”>Card Number:</label>
<input type=”text” id=”card-number” class=”form-control” readonly>
</div>
</form>
</div>

<script>
function generateCardNumber() {
const cardType = document.getElementById(‘card-type’).value;
let cardNumber = ”;

switch (cardType) {
case ‘visa’:
cardNumber = generateVisaCardNumber();
break;
case ‘mastercard’:
cardNumber = generateMastercardNumber();
break;
case ‘amex’:
cardNumber = generateAmexCardNumber();
break;
default:
cardNumber = ‘Invalid Card Type’;
break;
}

document.getElementById(‘card-number’).value = cardNumber;
}

function generateVisaCardNumber() {
const prefix = ‘4’;
const length = 16;
const randomNumber = generateRandomNumber(length – 1);
const cardNumber = prefix + randomNumber;

return cardNumber;
}

function generateMastercardNumber() {
const prefix = ‘5’;
const length = 16;
const randomNumber = generateRandomNumber(length – 1);
const cardNumber = prefix + randomNumber;

return cardNumber;
}

function generateAmexCardNumber() {
const prefix = ‘3’;
const length = 15;
const randomNumber = generateRandomNumber(length – 1);
const cardNumber = prefix + randomNumber;

return cardNumber;
}

function generateRandomNumber(length) {
let randomNumber = ”;
for (let i = 0; i < length; i++) {
randomNumber += Math.floor(Math.random() * 10);
}
return randomNumber;
}
</script>
</body>
</html>

Leave a Comment