View Full Version : Validate two Forms with one script

Gary Williams
03-31-2003, 07:55 PM
I have two similar forms on a web site that that can be validated by one javascript. How do I pass the name of the form to the script and then test which form is calling it?



03-31-2003, 08:04 PM
You don't generally pass a form's name to a script - you pass the Form object directly...more details as to the situation will get you a better answer. :)

03-31-2003, 08:22 PM
Normally, you do as cheesebagpipe just said - you pass a reference to the form object, not the name of it. This you do by putting an onsubmit attribute on the form tag, like this: <form [...] onsubmit="return fnValidateForm(this);">

The script:
function fnValidateForm(form){
| In here, you access the form elements
| by form.elements[[string nameOfFormElement]].
| You return true if you want to continue with the
| submission, or false if you want to abort it.

03-31-2003, 08:40 PM
ya, like fValidate (http://www.peterbailey.net/fValidate/)


03-31-2003, 09:04 PM
[ho'] Hey, don't steal my glory! [/ho']

If there's two things I've never been good at in JavaScript, it's form handling, including validation, and frames.

03-31-2003, 10:01 PM

Well good, then my next (http://www.peterbailey.net/fValidate/site.php?page=status) fValidate should be your cup of tea. It's obect oriented and will handle complex validations easily :)

03-31-2003, 10:36 PM
(Well, I don't like to use other's code - they seldom write my style. Noi, just truth)

On the other hand, object orientation is something I'm good at, and regex (http://wsabstract.com/javatutors/redev.shtml)'s are also (http://evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/) in my field of speciality.

03-31-2003, 11:00 PM
No insult taken. In fact, if I do borrow some code, the first thing I do is 'clean' it to my liking/style/taste/whatever ;)

Gary Williams
03-31-2003, 11:15 PM
Hi Guys,

Thanks for the advice but I haven't explained myself very well. Here goes again.

I have two forms to validate. A Quote form to submit to a database and an Edit form to recall and modify the stored data.

The button (1), at the end of the Quotation Form calls the function (2) setstatusandsubmit which sets the hidden field "strcurrentstatus" to hold the value 'continue'.

The same button (1a), at the end of the Edit Form calls the function (2) setstatusandsubmit which sets the hidden field "strcurrentstatus" to hold the value 'saveandexit'.

Once this is done, a function submitIt(formname) is called and, being part of an 'included' file is found and processes the 'Form' on the quotation page called 'formname'.

1. <INPUT TYPE=BUTTON NAME="btnsubmitquote" onclick="setstatusandsubmit('continue');">

1a. <INPUT TYPE=BUTTON NAME="btnstoreedit" onclick="setstatusandsubmit('saveandexit');">

2. function setstatusandsubmit(strcurrentstatus) {
formname.currentstatus.value = strcurrentstatus;

I need to test the value of (formname) as the first part of the validation and set a variable 'form' to equal either 'quote' or 'edit'. I can then select which set of form elements to test and validate.

Hope this makes sense.



03-31-2003, 11:22 PM
Try using this as the argument sent from the buttons, and retrieving the name from that attribute:
function setandsubmit(elm){
case 'btnsubmitquote':
/* run functions or expressions
related to quotes here*/
case 'btnstoreedit':
/* run functions or expressions
related to editing here*/
elm.form.submit(); // if this is what you want to do.
return true; // Unless you want to cancel the event

03-31-2003, 11:30 PM
Hmm, wait a moment - I just reread what you wrote up there - do you mean you have two different forms as in that you have two different form elements?

If so, why don't you put these submit buttons inside their respective forms, and change their type attribute to submit?
You really shouldn't have submit buttons that aren't really submit buttons, but if you have, you should at least put them in their respective forms.

03-31-2003, 11:31 PM
I got a bit of a headache reading the above; could be just my head but the program structure you've outlined seems convoluted. Anyway: why not just pass the button (Input) object - as mentioned - and let the function figure it out?

1. <INPUT TYPE=BUTTON NAME="continue" value="Continue" onclick="setstatusandsubmit(this);">

1a. <INPUT TYPE=BUTTON NAME="saveandexit" value="Save and Exit" onclick="setstatusandsubmit(this);">

function setstatusandsubmit(oButton) {
var oForm = oButton.form;
oForm.currentstatus.value = oButton.name;

Stuck some data in the button's name property. Lost you here:
I need to test the value of (formname) as the first part of the validation and set a variable 'form' to equal either 'quote' or 'edit'. Would help to know what you're validating (and what routine you're using).

Gary Williams
03-31-2003, 11:31 PM
Thanks liorean,

I think I follow this. Time for me to research 'case' and 'switch'



04-01-2003, 12:17 AM
Oh, it's just a way to write a multiple conditional (a'la if...elseif...else) that compares to the same object. What it in effect does is:

1. Evaluate the argument of switch
2. Compare the label (doesn't have to be a string) of the case to that argument.
3. Execute case if match, jump to next case if not.
4. If case executes and it includes a break statement, terminate the switch, when encountering the break statement otherwise repeat from 2.
5. If last case has passed but there is a default label left, execute default.

What I wrote is a more effective way of writing:
/*quote-related functionality */
}else if(elm.name=='btnstoreedit'){
/*edit-related functionality */