LegendIsReal LegendIsReal - 6 months ago 10
HTML Question

Javascript - how do I focus to field producing an error?

Working on an exercise of creating a credit card form and I'm a bit stuck. My issue is that it seems to always focus on the first field (first name), otherwise on the last field entered. Is there a simple way to have it focus on the appropriate field that is producing the error?

HTML:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Javascript Form Assignment</title>
</head>
<body bgcolor="E6E6FA">
<script src="./js/assignment.js">
</script>
<section>
<h2>Customer Details</h2>
<form name="contactForm" onsubmit="return validateForm()" method="post" >
First Name
<input id="fname" type="text" size="30">
<br /><br />
Last Name
<input id="sname" type="text" size="30">
<br /><br />
Postcode
<input id="pcode" type="text" size="30">
<br /><br />
Email
<input id="email" type="text" size="30">
<br />
<h2>Payment Details</h2>
Credit Card
<select name="creditcard">
<option value="amex">American Express</option>
<option value="visa">Visa</option>
<option value="master">Mastercard</option>
</select>
<br /><br />
Card number
<input id="cardnum" type="text" size="30">
<br /><br />
CCV
<input id="ccv" type="text" size="30">
<br /><br />
Expiry MM/YYYY
<select id="expiry_month" name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="expiry_year" name="year">
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<br />
<input type="submit" value="Submit"/>
<input type="button" value="Help" onclick="openWin()">
<br />
</form>
</body>
</html>





And my Javascript:



function openWin() {
window.open(src="./html/help.html");
}
function validateForm()
{
var fname = document.getElementById("fname");
var sname = document.getElementById("sname");
var pcode = document.getElementById("pcode");
var email = document.getElementById("email");
var cardnum = document.getElementById("cardnum");
var ccv = document.getElementById("ccv");

var expiry_month = document.getElementById("expiry_month").value;
var expiry_year = document.getElementById("expiry_year").value;

var today = new Date();
var selDate = new Date()

if (fname.value==""){
alert("Please enter your first name");
fname.focus();
return false;
}
if (sname.value==""){
alert("Please enter your last name");
fname.focus();
return false;
}
if (pcode.value==""){
alert("Please enter your postcode");
fname.focus();
return false;
}
if (pcode.value.length!=4 || isNaN(pcode.value)){
alert("Please enter a 4 digit postcode");
pcode.focus();
return false;
}
if (email.value==""){
alert("Please enter your email address");
fname.focus();
return false;
}
if (email.value.indexOf("@")==-1){
alert("Please enter a valid email address");
email.focus();
return false;
}
if (email.value.indexOf(".")==-1){
alert("Please enter a valid email address");
email.focus();
return false;
}
if (cardnum.value==""){
alert("Please enter your card number");
fname.focus();
return false;
}
if (cardnum.value.length!=16 || isNaN(cardnum.value)){
alert("Please enter a 16 digit credit card number");
cardnum.focus();
return false;
}
if (ccv.value==""){
alert("Please enter your ccv");
fname.focus();
return false;
}
if (ccv.value.length!=3 || isNaN(ccv.value)){
alert("Please enter a 3 digit CCV");
ccv.focus();
return false;
}
if (today.getTime() > selDate.setFullYear(expiry_year, expiry_month)){
alert ("Expiry month and year is before today month and year.");
return false;
}

alert("Thank you for your submission");
return true;
}





Sorry for the long post, still learning my way around here!

Answer

Your problem is right here:

if (sname.value==""){
        alert("Please enter your last name");
        fname.focus();
        return false;
}

You're checking the last name field and then focusing the first name field if it's invalid. It should be this:

if (sname.value==""){
    alert("Please enter your last name");
    sname.focus();
    return false;
}
Comments