SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Help with js form validation
-
Nov 3, 2003, 09:47 #1
- Join Date
- Oct 2003
- Location
- North Wales
- Posts
- 154
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help with js form validation
Sorry for submitting such a simple query, but I'm not too hot on javascript!
I want to validate a form so that if there is an error, an error message will appear in another input field. I've used the following code, but it doesn't work so there's clearly something wrong:
Code:if (theForm.surname1.value == "") { document.write.surname1error("Please enter a value"); theForm.surname1.focus(); return (false); } return (true);
-
Nov 3, 2003, 10:22 #2
- Join Date
- May 2003
- Posts
- 1,843
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:if (theForm.surname1.value == "" ) { theForm.surname1error.value = "Please enter a value"; theForm.surname1.focus(); return false; } return true;
The document.write() function is a very generalized way to output HTML dynamically; you don't use it after the page is loaded.::: certified wild guess :::
-
Nov 3, 2003, 11:40 #3
- Join Date
- Oct 2003
- Location
- North Wales
- Posts
- 154
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks, adios.
Originally Posted by adios
-
Nov 3, 2003, 12:44 #4
- Join Date
- May 2003
- Posts
- 1,843
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Nah, we hate 'em too. Who wants to be 'alerted'?
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>untitled</title> <script type="text/javascript" language="javascript"> var req_fields = ['firstname1' , 'middlename1' , 'surname1']; function checkform(oForm) { for (var field, bad = 0, f = 0; f < req_fields.length; ++f) { field = oForm.elements[req_fields[f]]; if (field.value == field.defaultValue || /^\s*$/.test(field.value)) { field.value = field.defaultValue; field.style.border = '2px #f00 dashed'; bad = 1; } } if (bad) return false; else return true; } function restore(oText) { oText.style.border = '2px #000 solid'; } function restoreAll(oForm) { for (var field, f = 0; f < req_fields.length; ++f) { field = oForm.elements[req_fields[f]]; restore(field); } } </script> </head> <body style="margin:100px;"> <form name="theForm" action="javascript:alert('ok')" onsubmit="return checkform(this)" onreset="restoreAll(this)"> <input id="firstname1" name="firstname1" type="text" style="width:156px;padding:2px;border:2px #000 solid;" value="Please enter a value here." onclick="if(this.value==this.defaultValue)this.value='';restore(this)" onkeypress="restore(this)">___first name<br /> <input id="middlename1" name="middlename1" type="text" style="width:156px;padding:2px;border:2px #000 solid;" value="Please enter a value here." onclick="if(this.value==this.defaultValue)this.value='';restore(this)" onkeypress="restore(this)">___middle name<br /> <input id="surname1" name="surname1" type="text" style="width:156px;padding:2px;border:2px #000 solid;" value="Please enter a value here." onclick="if(this.value==this.defaultValue)this.value='';restore(this)" onkeypress="restore(this)">___last name<br /><br /> <input type="submit" value="submit"> <input type="reset" onclick="return confirm('Clear all entries?')"> </form> </body> </html>
Last edited by adios; Nov 4, 2003 at 12:06.
::: certified wild guess :::
-
Nov 4, 2003, 03:33 #5
- Join Date
- Oct 2003
- Location
- North Wales
- Posts
- 154
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ah, now that's much better. I'm not sure that I completely understand the code, though. Would I need to repeat this bit for each of the fields to be validated?:
Originally Posted by adios
-
Nov 4, 2003, 12:08 #6
- Join Date
- May 2003
- Posts
- 1,843
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not sure that I completely understand the code either.
Nevertheless...edited the above. Alert!::: certified wild guess :::
-
Nov 5, 2003, 03:47 #7
- Join Date
- Oct 2003
- Location
- North Wales
- Posts
- 154
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by adios
Thanks for that, adios - most helpful.
-
Nov 5, 2003, 04:42 #8
Bookmarks