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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specific Form Validation

Status
Not open for further replies.

markhfla

Programmer
Oct 24, 2003
3
US
QUESTION:

If I have multiple forms with identical form field names on the same page, how can I validate them individually? Or, better yet, how do I pass the form field name to the JS and only validate that particular form?

SCENARIO:

I have a form within a PHP function that is included on the page as a blank form for new entries AND ALSO is dynamically recreated with the fields filled out from past entries (so that they can be edited).

Each form has a unique form id and name (i.e. form1, form2, etc.)

Each form has the following:

<form action="mypage.php" method="post" name="Unique_name" id="Unique_id" onSubmit="JavaScript:MM_validateForm('name','','R');return document.MM_returnValue"

My javascript is as follows (Dreamweaver generated):

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}

WHAT IS HAPPENING:

As it is now, when I try to edit one of the forms that have been dynamically filled in, I get the alert window because I have the same fields in an identical empty form on the same page.
 
Give your forms ids as well as names. That way you can reference the form you want with

document.getElementById("formID").elements["fieldName"].value

Remember, ID names have to be unique.

[monkey][snake] <.
 
Each form does have a unique id.

Where do I put document.getElementById("formID").elements["fieldName"].value within my existing script?
 
[1]
><form action="mypage.php" method="post" name="Unique_name" id="Unique_id" onSubmit="JavaScript:MM_validateForm('name','','R');return document.MM_returnValue"

Must be in literal agreement there.

[tt]<form action="mypage.php" method="post" name="[blue]Unique_name[/blue]" id="Unique_id" onSubmit="JavaScript:MM_validateForm('[red]Unique_name[/red]','','R');return document.MM_returnValue"[/tt]

[2] Make sure that the MM's included function [tt][blue]MM_findObj()[/blue][/tt] (that I don't know what) returns that the object (probably referencing the same form) with a sensible "value" attribute that you're going to validate, otherwise, the majority of validating process is not applying.
 
[1a] The literal agreement can always be achieved in that structure by "this" reference.
[tt]
<form action="mypage.php" method="post" name="Unique_name" id="Unique_id" onSubmit="JavaScript:MM_validateForm([blue]this.name[/blue],'','R');return document.MM_returnValue"
[/tt]
(The form is incomplete (>) in the op's post, no attempt to correct that here.)
 
If you haven't already guessed, I know little to nothing about Javascript!

I tried your suggestion in 1a, but it didn't work.

I believe the getElementByID() is the way to go. But, I don't know where to put it in my code above AND how can I pass the form id to getElementByID() as a variable?

Just to clarify, the blank form and the filled in forms on my page are identical except for the form ids and names. I want to tell the validation javascript (MM_validateForm) to only validate the form that I'm submitting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top