Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation with

Status
Not open for further replies.

dontpunchme

Programmer
Jul 29, 2002
20
US
I am trying to validate a form.... if the form isn't valid... rather than popping an alert box.... I would like to write a line of text (document.write) an error message in the form where the error showed up.

current script:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- //

function checkInput2(form)
{
if (form.elements['sp-q'].value == &quot;&quot; || form.elements['sp-q'].value == &quot;null&quot;)
{
alert(&quot;\n Please fill in the box below.)&quot;);
return false;
}
else
{
return true;
}
}
// -->
</SCRIPT>


Form:
<form method=&quot;get&quot; name=&quot;offer_form&quot; action=&quot;#&quot; onSubmit=&quot;return checkInput2(this)&quot;>
Name: <input size=10 name=&quot;sp-q&quot;>
</form
 
I had to do this once and remember the quickest way I thought of was to create boxes above each input field and have no border shown to it appeared they were not there. then on error I filled the field with the error allocating it above the field in error.

I know there has got to be a easier way to dynamicaly write to the screen in javascript though. this is just one option A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
I wrote this example for you to see what I meant

<html>
<head>
<script>
function Val(){
if (frm.txt1.value == &quot;&quot;) {
frm.txt1error.value = &quot;Error in this box!&quot;
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form name=&quot;frm&quot; onSubmit=&quot;return Val()&quot;>
<table>
<tr><td>
<input type=&quot;text&quot; name=&quot;txt1error&quot; style=&quot;border:none;color:red;&quot;>
<input type=&quot;text&quot; name=&quot;txt1&quot;>
</td></tr>
<tr><td colspan=2><p align=right>
<input type=&quot;submit&quot; value=&quot;submit&quot;></p>
</td></tr>
</table>
</form>
</body>
</html> A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
I'd also add &quot;disabled&quot; property to <input type=&quot;text&quot; name=&quot;txt1error&quot;> so the user will not type anything in it accidentially.

You can also use innerHTML or innerText, but it has less support among different browsers and therefore I don't think it's a good idea to limitate form functionality bacause of that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top