Very Basic CC Validation Javascript

Joined
Oct 29, 2019
Messages
1
Reaction score
0
Ok so I could use some help figuring out why this code wont run. Im pretty terrible with JavaScript just started learning. Also id like to stay away from the unnecessary coding with difficult to understand symbols as much as possible and try to keep to a minimal amount of lines.


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lesson 7 Application Project</title>

<script language="javascript">


function validate(form) {

var cardLength = form.number.value.length;
var cardValue = form.number.value;
var visaIndex = cardValue.indexOf("4",0);
var masterIndex = cardValue.indexOf("5",0);
var amexIndex = cardValue.indexOf("3",0);
// var visaValue = form.ccard[0].checked;
// var masterValue = form.ccard[0].checked;
// var amexValue = form.ccard[0].checked;
var monthLength = form.expMonth.length;
var yearLength = form.expYear.length;
var today = new Date();
var curYear = today.getFullYear();
var curMonth = today.getMonth();





// for (var x =0 < monthLength; x++;){
// var monthSelect = (form.expMonth[x].value);
// if (form.expMonth.options[x].selected);
// alert("First For loop");

// for (var i= 0 < yearLength; i++;){
// var yearSelect = (form.expYear.options.value);
// if (form.expYear.options.selected);
// alert("Second For loop");
// }


// if (yearSelect < curYear){
// alert("Invalid Year.");
// return false;
// }
// else if (yearSelect<=curYear && monthSelect<=curMonth){
// alert("PLaes");
// return false;
// }
// else if (visaValue == false && masterValue == false && amexValue == false){
// alert("Enter valid card number and select correct type.");
// return false;
// }
// else if (cardLength < 1){
// alert("Card Number Invalid");
// return false;
// }

// else if (visaValue == true){
// if (cardLength != 16 || visaIndex !=0){
// alert("Please enter valid Visa Number");
// }
// }

// else if (masterValue == true){
// if (cardLength != 15 || visaIndex !=0){
// alert("Please enter valid Visa Number");
// }
// }

// else if (amexValue == true){
// if (cardLength != 14 || visaIndex !=0){
// alert("Please enter valid Visa Number");
// }
// }
// else {
// alert("Transaction Success");
// }

}

</script>

</head>

<body>

<h3>CIW JavaScript Specialist</h3>
<hr />
<form name="ccForm">
<p>
<input type="radio" name="ccard" value="visa" />
Visa
</p>
<p>
<input type="radio" name="ccard" value="mc" />
MasterCard
</p>
<p>
<input type="radio" name="ccard" value="amex" />
American Express
</p>
<p>
Card #
<input type="text" name="number" maxlength="16" minlength="15"/>
</p>
<p>
Exp Date:
<select name="expMo">
<option value=0>Jan
<option value=1>Feb
<option value=2>Mar
<option value=3>Apr
<option value=4>May
<option value=5>Jun
<option value=6>Jul
<option value=7>Aug
<option value=8>Sep
<option value=9>Oct
<option value=10>Nov
<option value=11>Dec
</select>
<select name="expYr">
<option value=2016>2016
<option value=2012>2017
<option value=2013>2018
<option value=2014>2019
<option value=2015>2020
<option value=2016>2021
<option value=2017>2022
<option value=2018>2023
</select>
</p>

<input type="button" value="Check Form" onClick="validate(this.form);" />
<input type="reset" value="Reset Form" />
</form>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top