/    Sign up×
Community /Pin to ProfileBookmark

Form Validation – Highlight Text Field

Ok, I have included all the code that I have built for this page, in the post and as a text file attachment. It currently checks the form for blank fields, and if it has any blank fields, it then give a pop up message. Could someone show me how to use DHTML to change the labels next to the inputs, highlighted in red and bold and focus in the first bad field when the form is submitted, rather than using the pop ups. After you make changes to the field it will go back to normal colors, and recheck the form again on submit for any more errors.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<title>Lab 04</title>
<style type=”text/css”>
<!–
body,td,th {
font-family: Arial, Helvetica, sans-serif;
}
.formBold {
font-size: small;
font-weight: bold;
}
.form {
font-size: x-small;
font-weight: bold;
}
.small {
font-size: xx-small;
font-family: Arial, Helvetica, sans-serif;
}
–>
</style>
<script>
<!–
function checkForNumber(fieldValue) {
var numberCheck = isNaN(fieldValue);
if (numberCheck == true) {
window.alert(“You must enter a numeric value for your ZIP Code!”);
return false;
}
}
function confirmPassword() {
if (document.Info.Password.value != document.Info.passwordVerify.value) {
window.alert(“You must use use the same password in the verification field!”);
document.Info.Password.focus();
}
}
function ensurePasswordConfirm() {
if (!document.Info.passwordVerify.value) {
window.alert(“You must confirm the password you entered!”);
document.Info.passwordVerify.focus();
}
}
function checkCheckBox(formName) {
if (formName.checkPolicy.checked == false) {
window.alert(“You must agree with the policies of this site by checking the checkbox, before continuing.”);
return false;
} else {
return true;
}
}
function blankTextFieldValidator() {
if (document.Info.fName.value == “”
|| document.Info.mName.value == “”
|| document.Info.lName.value == “”) {
window.alert(“You must enter your name.”);
return true;
}
else if (document.Info.Address.value == “”
|| document.Info.City.value == “”
|| document.Info.State.value == “”
|| document.Info.Zip.value == “”) {
window.alert(“You must enter your address, city, state, and zip code.”);
return true;
}
else if (document.Info.Username.value == “”
|| document.Info.Password.value == “”
|| document.Info.passwordVerify.value == “”) {
window.alert(“You must enter your desired username and password.”);
return true;
}
return false;
}
function confirmSubmit() {
blankTextFieldValidator();
checkCheckBox(document.Info);
}
function confirmReset() {
var resetForm = window.confirm(“Are you sure you want to reset the form?”);
if (resetForm == true)
return true;
return false;
}
–>
</script>
</head>

<body>
<h1>Lab 04</h1>
<hr width=”100%” noshade=”noshade” />
<form name=”Info” id=”Info” method=”get” onsubmit=”return confirmSubmit();” onreset=”return confirmReset();”>
<table width=”35%” cellspacing=”0″>
<tr bgcolor=”#0099FF”>
<td colspan=”3″ class=”form”>&nbsp;</td>
</tr>
<tr align=”center” bgcolor=”#999999″>
<td height=”36″ colspan=”3″ class=”formBold”>Please enter all your information
in the boxes below:</td>
</tr>
<tr>
<td width=”181″ class=”form”> <p>First Name:<br />
<input name=”fName” type=”text” id=”fName” />
</p></td>
<td width=”60″ class=”form”> <p>M.I.:<br />
<input name=”mName” type=”text” id=”mName2″ size=”2″ maxlength=”1″ />
</p></td>
<td width=”431″ class=”form”>Last Name:<br /> <input name=”lName” type=”text” id=”lName2″ />
</td>
</tr>
<tr>
<td colspan=”3″ class=”form”> <p>Street Adress:<br />
<input name=”Address” type=”text” id=”Address2″ size=”60″ />
</p></td>
</tr>
<tr>
<td class=”form”>City:<br /> <input name=”City” type=”text” id=”City2″ /></td>
<td class=”form”> <p>State:<br />
<input name=”State” type=”text” id=”State2″ size=”3″ maxlength=”2″ />
</p></td>
<td class=”form”>Zip Code:<br /> <input name=”Zip” type=”text” id=”Zip2″ size=”10″ maxlength=”5″ onchange=”return checkForNumber(this.value);” />
</td>
</tr>
<tr>
<td colspan=”3″><span class=”form”>Username:</span> <span class=”small”>(7-24
characters)<br />
</span> <input name=”Username” type=”text” id=”userName2″ size=”26″ maxlength=”24″ />
</td>
</tr>
<tr>
<td colspan=”2″> <p><span class=”form”>Password:</span> <span class=”small”>(6-12
characters)</span>
<input name=”Password” type=”password” id=”Password2″ maxlength=”12″ onchange=”ensurePasswordConfirm();” />
<br />
</p></td>
<td><span class=”form”>Password:</span> <span class=”small”>(verify)</span><br />
<input name=”passwordVerify” type=”password” id=”passwordVerify2″ maxlength=”12″ onchange=”confirmPassword();” />
</td>
</tr>
<tr>
<td colspan=”2″ class=”form”>I agree to the terms and conditions in the
policy:</td>
<td class=”form”> <input type=”checkbox” name=”checkPolicy” value=”Yes” /></td>
</tr>
<tr>
<td colspan=”2″ align=”right”>
<input type=”reset” name=”Reset” value=”Reset” /></td>
<td align=”left”><input type=”submit” name=”Submit” value=”Submit” /></td>
</tr>
</table>
</form>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarMar 18.2005 — I don't know how to change the background color(unless your planning a complexe rewrite) but you can change text color and make it focused:

using your first name text box as an example:

if(fName.value == dodgy){

firstName.style.color = "red";

fName.focus();

};

</script>

<td width="181" class="form"> <p id=firstName>First Name:<br />

<input name="fName" type="text" id="fName" onchange="firstName.style.color='black';" />

</p></td>
Copy linkTweet thisAlerts:
@maximu5Nov 26.2008 — Sorry to bring this OLD OLD OLD OLD thread back to life but Im working on a sample form that i will probably be using for a website, could someone use that same code he has since and show how about to doing what the OP was talking about? thank you very much and i thought it would be better just to resurrect this thread rather than to start a new one, Thank you!
×

Success!

Help @critchey1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...