Validate only 1 of 2 forms on the same page?

M

mirkobonet

Hi,
I created two identical forms on the same page. I'd like to validate
only the one I'm in when I click submit.

I'm using the javascript

function MM_validateForm() { //v4.0
if (document.getElementById){
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=document.getElementById(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 == '');
} }

and

<input type='submit'
onclick="MM_validateForm('Email_address','','RisEmail','Preferred_Phone_Number','','R');return
document.MM_returnValue" value='Submit'>
&nbsp;&nbsp; <input type='button' value='Cancel'
onClick="location.href='/';">

I add the form ID, but I don't know where to put in the script.

One solution would be rename all the fields with different names, but
I don't want to do that otherwise I need to change the php action
script too.

It should be a way to validate just a specific form, right?

Thanks all!
 
S

SAM

(e-mail address removed) a écrit :
Hi,
I created two identical forms on the same page. I'd like to validate
only the one I'm in when I click submit.

Normalement il n'y a pas besoin de Javascript pour ça.

Usually no Javascript is needed.

You give a specific name to each submit-button
or much better you give the same name, but with specific values.

Server's code then analyzes this button's value and know witch form was
sent.

Using this way of doing, you can also have in the same form several
submit-buttons with same names but different values, the php receives
the clicked submit-button's value to know what he'll have to do.
I'm using the javascript

function MM_validateForm() { //v4.0

pfffftttt ! once again a MacroMerde function ?
why do you work with IDs instead of names ?

If you really want to verify the submitted form before to send it, you
have to :
- get an onsubmit in the form's tag
and not use an onclick in the submit-button
- the onsubmit will have to return true if all is correct
and false if not

function MM_validateForm(myForm) { // V.0.SAM
var errors = '',
nm, val, p, num, min, max, messg;
var f = myForm.elements;
for(var i=0; i<f.length-2; i++) {
nm = f.name;
val = f.value;
messg = 'The element '+nm+' ';
if(val=="" || val.length<1)
errors += messg+'is empty.\n';
else
if(nm.indexOf('isEmail')!=-1) {
p = val.indexOf('@');
if(p<1 || p==(val.length-1))
errors += messg+'must contain a valid e-mail address.\n';
}
else
if(nm.indexOf('R')!=0) {
if((isNaN(val))
errors += messg+'must contain a number.\n';
if (nm.indexOf('inRange') != -1) {
p = nm.indexOf(':');
num = parseFloat(val);
min = nm.substring(8,p);
max = nm.substring(p+1);
if (num<min || max<num)
errors += messg+'must contain a number between '+
min+' and '+max+'.\n';
}
}
else
if(!f.checked) errors += messg+'is required.';
}
myForm.MM_returnValue.value = 'errors :\n--------\n'+errors;
myForm.MM_returnValue.style.display = '';
if(errors!='') {
alert('errors :\n'+errors);
myForm.MM_returnValue.style.display = 'block';
return false;
}
return true;
}

and

<form onsubmit="return MM_validateForm(this);" action=" ...blah ...>

<textarea name="MM_returnValue" style="display:none"></textarea><br>
<input type='submit' value='Submit'>
&nbsp;&nbsp;
<input type='button' value='Cancel' onClick="location.href='/';">
 
C

Camo

Remember that ids should be unquie for a html document, so you the
elements you want to validate to need to be all unquie.

One soultions includes, prefix the form fields with the id of the
field

Eg.
In form 1
ID="form1_email"

In form 2
ID="form2_email"

The you can pass in the id of the form to the function and do
something like document.getElementById(formID + "_" + fieldName");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,985
Messages
2,570,202
Members
46,777
Latest member
JosefinaAc

Latest Threads

Top