|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
form name problem
Hello
I have a script which validates form fields and uses: <form name="Sample1" method="post"> Once I have completely the form fields correctly (if I don't I get an error message in red), and click the Submit button, the page doesn't lead anywhere (I don't have a redirect on the page). On the other hand, if I remove the abover and substutite this: <form name="thanks" method="POST" action="thanks.asp"> and click the Submit button, it does take me to the "thank you" page, BUT DOES NOT VALIDATE THE FIELDS. How can I combine the two? Many thanks for any advice. Hatter |
|
#2
|
|||
|
|||
|
javascipt validation
use javascript for validation and use the onSubmit event handler in your form.
This eventhandler will be executed before the page changes. Small example: Code:
<script language="JavaScript" type="text/javascript">
function FormValidation(oForm) {
if (oForm.textfield.value.length == 0 ) {
alert ("please enter a value");
oForm.textfield.focus();
return false; }
}
</script>
<form action="handleForm.asp" method="post"
onSubmit="javascript: return FormValidation(this)"
name="myForm">
<input type="text" name="textfield">
<input type="submit" name="cmdSubmit" value="submit">
</form>
for more help on validating fields using javascipt, please post questions in the javascript forum. Hope this helps,
__________________
- Rogier Doekes |
|
#3
|
||||
|
||||
|
In your first snippet, you didn't give the form an action. In the second, it has an action but not the same name as in the first snippet. My guess is that your validation script cares that the form is named and so won't work when you use the second snippet. But since the first snippet has no action parameter, there's nothing for it to do. Just add the action parameter to your first snippet and I suspect you'll be good to go.
|
|
#4
|
||||
|
||||
|
Following on from what dhouston has said, creating modular validation scripts is made easier if you don't use specific form names. (To accomplish this, keep your JS validation code in an external *.js file, and link it using src="blah.js") To pass the form object as a parameter, do something like this:
Code:
<script language="Javascript" type="text/javascript">
function validate(theForm) {
varFname = theForm.fname.value
if (varFname == "") {
//err...
}
}
</script>
<form name="frmRego" action="reg.asp" method="post" onSubmit="return validate(this)">
<input type="text" name="fname">
</form>
|
|
#5
|
||||
|
||||
|
Yeah, stumpy's definitely onto something. A while back, I actually wrote a portable and pretty much all-purpose validation js that I can use with any page. I include it at the top of the needed files as stumpy proposes above. Then, above my form, I write a little inline js that defines an array of field names, types, and error messages. I pass these along with the form itself to my js onsubmit, and the script does the appropriate validation based on the form type and returns the appropriate error message, etc. The more general the validation code the better.
|
|
#6
|
||||
|
||||
|
I once saw a very groovy bit of JS very similar to what dhouston is talking about, a few years ago. It stored all the checking info (check type [range checking, etc], error message, data types, etc) in hidden fields - almost exactly like how .NET works now.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > form name problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|