Skip to content

Added validation before checkout button click #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,32 @@
<label style="color:whitesmoke;font-weight: 600;">Card Number</label><br />
<input type="number"
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;"
id="card" />
id="cardNo" required/>
</td>
</tr>
<tr>
<td colspan="2">
<label style="color:whitesmoke;font-weight: 600;">Card Holder</label><br />
<input type="text"
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" />
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" id="cardHolder" required />
</td>
</tr>
<tr>
<td style="width: 50%;">
<label style="color:whitesmoke;font-weight: 600;">Expires</label><br>
<input type="date"
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" />
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" id="expires" required />
</td>
<td style="width:50% ;padding-left: 20px;">
<label style="color:whitesmoke;font-weight: 600;">CVC</label><br>
<label style="color:whitesmoke;font-weight: 600;">CVV</label><br>
<input type="number"
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" />
style="height: 30px;width: 100%;background-color:rgb(91, 177, 253);border: none;color: white;outline: none;" id="cvv" required />
</td>
</tr>
<tr>

<td colspan="2" style="padding-top: 70px;padding-bottom: 10px;" id="td-button">
<a href="#" class="btn hvr-right" onclick= "check();">Checkout</a>
<button type="submit" class="btn hvr-right">Checkout</button>
</td>
<div id="popup-modal" class="modal">
<div class="modal-content animated bounce">
Expand Down
72 changes: 60 additions & 12 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
let card = document.getElementById("card").value;
var form = document.querySelector("form");

var modal = document.getElementById('popup-modal');
var span = document.getElementsByClassName("modal-close")[0];

window.onload = function() {
setTimeout(function() {
modal.style.display = 'block';
}, 3000);
}

window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}

function check() {
modal.style.display = "block";
}

span.onclick = function() {
modal.style.display = "none";
}
Expand All @@ -34,4 +25,61 @@ function changeFunc() {
document.getElementById("cardlogo").src = "https://www.aexp-static.com/cdaas/one/statics/axp-static-assets/1.8.0/package/dist/img/logos/dls-logo-bluebox-solid.svg";
document.getElementById("cardlogo").style.height = "100px";
}
}
}

function validate_cardnumber(cardNo)
{
var visaCardRegex=/^(?:4[0-9]{12})(?:[0-9]{3})$/;
var americanExpCardRegex=/^(?:3[47][0-9]{13})$/;
if (visaCardRegex.test(cardNo) || americanExpCardRegex.test(cardNo)){
return true;
}

alert("Please enter valid card number!");
return false;
}

function validate_holdername(cardHolder) {
var holdername_regex = /^([a-zA-Z ]){2,30}$/;
if(holdername_regex.test(cardHolder)){
return true;
}

alert("Please enter valid card holder name!");
return false;
}

function validate_expiration(givenDate) {
var givenDate = givenDate;
var CurrentDate = new Date();
givenDate = new Date(givenDate);

if(givenDate > CurrentDate){
return true;
}

alert("Date shouldn't be less than today!");
return false;
}

function validate_cvv(cvv) {
var cvv_regex = /^[0-9]{3,4}$/;
if(cvv_regex.test(cvv)){
return true;
}

alert("Please enter valid CVV number!");
return false;
}

form.onsubmit = (e) => {
e.preventDefault();
var cardNo = document.getElementById("cardNo");
var cardHolder = document.getElementById("cardHolder");
var cvv = document.getElementById("cvv");
var expires = document.getElementById("expires");

if(validate_cardnumber(cardNo.value) && validate_holdername(cardHolder.value) && validate_cvv(cvv.value) && validate_expiration(expires.value)){
modal.style.display = "block";
}
};
3 changes: 2 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ input{
text-decoration: none;
width: 80%;
text-align: center;
height: 28px;
height: 3.1rem;
padding: 8px 0;
margin: 0 auto;
cursor: pointer;
}
.hvr-right{
display: inline-block;
Expand Down