Closed Thread Icon

Preserved Topic: mr.maX script with multiple forms Pages that link to <a href="https://ozoneasylum.com/backlink?for=18313" title="Pages that link to Preserved Topic: mr.maX script with multiple forms" rel="nofollow" >Preserved Topic: mr.maX script with multiple forms\

 
Author Thread
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 01-07-2002 03:49

I have been using mr.maX's form validation scripts which work very nicely of course, but I'm now trying to use them on a page with two forms and I'm not quite sure what I need to adjust to accomodate multiple forms on one page. One form just has a select box and submit button. The second form has three text fields, "date", "headline", and "content". Here's what I have tried, but isn't working:

// Written by mr.maX, http://www.max.co.yu/

function testOption(field, msg) {
if (field.selectedIndex == 0) {
alert(msg);
field.focus();
return false;
}
else
return true;
}

function testBlank(field, msg) {
if (field.value == "") {
alert(msg);
field.focus();
field.select();
return false;
} else
return true;
}

function checkDate(field, msg) {
var timeRe = /^([\d]{1,2}\/[\d]{1,2}\/[\d]{2})$/
if (!timeRe.test(field.value)) {
alert(msg);
field.focus();
field.select();
return false;
} else
return true;
}

function testAndSubmit(form) {
return (
testBlank(form.headline, "Headline (must not be blank)") &&
testBlank(form.content, "Article (must not be blank)") &&
checkDate(form.date, "Date (must not be blank and it's format must be MM/DD/YY )") &&
testOption(form.edit_headline, "You must select a headline to edit")
);
}

Thanks for your help!

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 01-07-2002 04:53

Hey butcher. Assuming you are using something like onSubmit=testandSubmit(this) (the this is the important part) to call the function you can simply call a different function depending on which form is being submitted. The this object sends a reference to the specific form so you don't have to do much rewriting. If you are naming your forms (<form name=form1...) you can put a simple if statement in your testandSubmit function.

code:
function testAndSubmit(form) {
if (form.name = "form1") { //Test submit fields
return testOption(form.SubmitFieldName)
} else { //test everything else
testBlank(form.headline, "Headline (must not be blank)") &&
testBlank(form.content, "Article (must not be blank)") &&
checkDate(form.date, "Date (must not be blank and it's format must be MM/DD/YY )") &&
testOption(form.edit_headline, "You must select a headline to edit")
)
} else {
}



I hope this helps it may be a little vague.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 01-07-2002 10:42

You can also write two different testAndSubmit() functions and call appropriate function on each form...

Something like this:

function testAndSubmitForm1(form) {
&nbsp;&nbsp;&nbsp;&nbsp;return ...
}

function testAndSubmitForm2(form) {
&nbsp;&nbsp;&nbsp;&nbsp;return ...
}

<FORM ONSUBMIT="return testAndSubmitForm1(this)">
first form
</FORM>

<FORM ONSUBMIT="return testAndSubmitForm2(this)">
second form
</FORM>




[This message has been edited by mr.maX (edited 01-07-2002).]

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 01-07-2002 20:59

Thanks guys!

I really appreciate you both taking the time to help me and share your knowledge.

-Butcher-

« BackwardsOnwards »

Show Forum Drop Down Menu