Hi All,
I am in a JavaScript college class and right now we are learning about form validation, well before server side validation, and I am having an issue with validating the field if the user updates it and hits submit again. The red .setAttribute function is not removed. My code is below, please excuse the noobness I am displaying here. We start learning JQuery next week so this may be moot by then…
My question is, what am I missing to make the form validate on the second submit?
<script type="text/javascript" language="javascript">
//<![CDATA[
window.onload = (function()
{
document.getElementById("fn1").focus();
function showerr(elmnt)
{
document.getElementById(elmnt).setAttribute("style","background-color:#ff0000;");
//attributes on page 327
}
function validate(f)
{
if(!f.first.value.match(/^\\w/)) //matches any character, entry \\d is digit
{
showerr("fn1");
}
if (!f.last.value.match(/^\\w/))
{
showerr("ln1");
}
return false
}
document.myform.onsubmit =
(
function()
{
return validate(this)
}
);
}
);
//]]>
</script>
None of those function bodies need to be inside the onload handler, but it makes no difference.
You know already how to set the background attribute, so just do it whenever the validate function is called.To restore a default CSS value, assign an empty string. Here’s a low-maintenance method:
function resetFieldBackgrounds( f )
{
var fe = f.elements;
for( var i = 0, len = fe.length; i < len; i++ )
if( fe[ i ].nodeName == 'INPUT' )
fe[ i ].style.backgroundColor = "";
}
function validate(f)
{
resetFieldBackgrounds( f );
...............
BTW your number validation is incorrect because the hyphen has a special use within , so either escape the hyphen \- and/or just don’t use .
What he means is that each time you validate, before the validation occurs you should first remove from the screen all of the error messages that may exist from previous validations.
Sorry, I am not sure I understand how to re-set the background-color before validation. Are you meaning to move the function showerr() outside the window.onload event handler?
Thanks.
Here is my new code:
<script type="text/javascript" language="javascript">
//<![CDATA[
window.onload = (function()
{
document.getElementById("fn1").focus();
function showerr(elmnt)
{
document.getElementById(elmnt).style.backgroundColor = "#ff0000";
//attributes on page 327
}
function validate(f)
{
if(!f.first.value.match(/^\\w/) || f.first.value.match(/\\d/)) //checks for any word entry at beginning of line or any digit in the field
{
showerr("fn1");
}
if(!f.last.value.match(/^\\w/) || f.last.value.match(/\\d/)) //checks for any word entry at beginning of line or any digit in the field
{
showerr("ln1");
}
if(!f.phone.value.match(/^\\d{3}[-]\\d{3}[-]\\d{4}/))
{
showerr("ph1");
}
return false
}
document.myform.onsubmit =
(
function()
{
return validate(this)
}
);
}
);
//]]>
</script>
</head>
<body>
<form name="myform">
<label id="lfn1" for="fn1">First Name:<br /></label>
<input type="text" id="fn1" name="first" /><br />
<label id="lln1" for="ln1">Last Name:<br /></label>
<input type="text" id="ln1" name="last" /><br />
<label id="lph1" for="ph1">Phone Number(111-222-3456):<br /></label>
<input type="text" id="ph1" name="phone" /><br />
Gender:<br />
<label for="chk1">Male</label>
<input type="checkbox" id="chk1" />
<label for="chk2">Female</label>
<input type="checkbox" id="chk2" />
<label for="chk3">Identity Crisis</label>
<input type="checkbox" id="chk3" />
<br /><br />
<input type="submit" name="submit" value="SUBMIT" id="" />
</form>
</body>
</html>