validate multiple forms with the same formname

C

Charlotte

Hi,

(sorry for my english)

I'm having a problem with multiple forms on the same page

all the fields in all the forms will be checked that they are not empty
like this:
<form action="..." name="FormOpleiding" onSubmit="return
CheckFormOpleiding();">

and the function:

function CheckFormOpleiding()
if (document.FormOpleiding.opleiding.value==""){
errorMsg += "xxxxxxx";
}
.... etc ...

must do the job

the problem is that the forms are placed in the page by a script with ASP
so there are several forms with the same formname
in this case with the formname -FormOpleiding-

how can I dynamicly give the forms an other name
and dynamicly check that form with a function

are is there an other solution ?

thanks

charlotte
 
E

Erwin Moller

Charlotte said:
Hi,

(sorry for my english)

I'm having a problem with multiple forms on the same page

all the fields in all the forms will be checked that they are not empty
like this:
<form action="..." name="FormOpleiding" onSubmit="return
CheckFormOpleiding();">

and the function:

function CheckFormOpleiding()
if (document.FormOpleiding.opleiding.value==""){
errorMsg += "xxxxxxx";
}
... etc ...

must do the job

the problem is that the forms are placed in the page by a script with ASP
so there are several forms with the same formname
in this case with the formname -FormOpleiding-

how can I dynamicly give the forms an other name
and dynamicly check that form with a function

are is there an other solution ?

Hi,

By far the best solution is NOT giving the forms the same name.
This should be a breeze for the ASP programmer that produced the forms
in the first place.
I am not sure, but I expect all forms should have different names
according to W3C guidelines, so there is another reason to rename them.

But if that is not an option, you could use javascript to rename them.

Something allong these lines (not tested):

Use an onLoad handler, and do the following:
- loop over all forms using:
var allForms = document.forms;
var numForms = allForms.length;
for (var i=0;i<numForms;i++){
allForms.name="FormOpleiding"+i;
}

Now the names are changed to FormOpleiding0,FormOpleiding1, etc.
And you can use these names to adres them.
Of course, you can also leave their names, and adres your forms using
their index, eg document.forms[0].

Not tested.
Hope that helps.

Regards,
Erwin Moller
 
S

SAM

Charlotte a écrit :
the problem is that the forms are placed in the page by a script with ASP
so there are several forms with the same formname
in this case with the formname -FormOpleiding-

how can I dynamicly give the forms an other name
and dynamicly check that form with a function

are is there an other solution ?

of course ...

To verify all the forms, use the forms's tree :


function verifForms() {
var D = document.forms;
for(var i=0; i<D.length; i++) {
var f = D.elements;
for(var k=0; k<f.length; k++)
if(f[k] && f[k].type=='text' && f[k].value.length<1) {
alert('Le champs "'+f[k].name+'" n\'est pas rempli');
f[k].focus();
f[k].select();
return false;
}
}
return true;
}



To verify only the submitted form, use the form's tree :

<script type="text/javascript">
function CheckFormOpleiding(what) {
if(typeof(what)=='undefined') what = this;
var f = what.elements;
for(var k=0; k<f.length; k++) {
if(f[k] && f[k].type=='text' && f[k].value.length<1) {
alert('Felder "' + f[k].name + '" ist nicht erfüllt');
f[k].focus();
f[k].select();
return false;
}
}
return true;
}
</script>

<form action="#" onsubmit="return CheckFormOpleiding(this);">
 
E

Evertjan.

Charlotte wrote on 30 okt 2007 in comp.lang.javascript:
(sorry for my english)

I am not.
I'm having a problem with multiple forms on the same page

all the fields in all the forms will be checked that they are not empty
like this:
<form action="..." name="FormOpleiding" onSubmit="return
CheckFormOpleiding();">

Het beste is:

<form action="..." name="FormOpleiding"
onSubmit="return CheckFormOpleiding(this);">
and the function:

function CheckFormOpleiding()
if (document.FormOpleiding.opleiding.value==""){
errorMsg += "xxxxxxx";
}

en dan:

function CheckFormOpleiding(f)
if (f.elements['opleiding'].value=="")
errorMsg += "xxxxxxx";
};

... etc ...

must do the job

the problem is that the forms are placed in the page
by a script with ASP
so there are several forms with the same formname
in this case with the formname -FormOpleiding-

Probeer dit eens met een formArray:

============= test.html ===============

<form name='FormOpleiding'>
<input>
</form>
<form name='FormOpleiding'>
<input>
</form>
<form name='FormOpleiding'>
<input>
</form>
<form name='FormOpleiding'>
<input>
</form>

<script type='text/javascript'>

var formArray = document.getElementsByName('FormOpleiding');

for (var i=0;i<formArray.length;i++)
formArray.getElementsByTagName('input')[0].value =
'Nummer '+i;

</script>

============================

Sorry for my Dutch, group.
 
C

Charlotte

<form action="..." name="FormOpleiding"
onSubmit="return CheckFormOpleiding(this);">

function CheckFormOpleiding(f)
if (f.elements['opleiding'].value=="")
errorMsg += "xxxxxxx";
};

Evertjan,
deze heb ik gebruikt met succes
bedankt
Charlotte

PS: ik kom u overal tegen
 
R

RobG

On Oct 30, 7:12 pm, Erwin Moller
[...]
By far the best solution is NOT giving the forms the same name.
This should be a breeze for the ASP programmer that produced the forms
in the first place.
I am not sure, but I expect all forms should have different names
according to W3C guidelines, so there is another reason to rename them.

Name attribute values do not need to be unique in a valid HTML
document.

But if that is not an option, you could use javascript to rename them.

Except that is problematic in IE, the revised name attribute is not
recognised within its DOM but will be used for successful controls.

Something allong these lines (not tested):

Use an onLoad handler, and do the following:
- loop over all forms using:
var allForms = document.forms;
var numForms = allForms.length;
for (var i=0;i<numForms;i++){
allForms.name="FormOpleiding"+i;

}

Now the names are changed to FormOpleiding0,FormOpleiding1, etc.
And you can use these names to adres them.


Not in IE.

Of course, you can also leave their names, and adres your forms using
their index, eg document.forms[0].

That is one solution. Another is to use one form with multiple submit
buttons that each have a unique name so the OP can tell which one was
used.
 
R

RobG

Unfortunately, both statements are far too general which makes them false.

I'm not sure what you mean by "both statements". I stand corrected in
regard to A elements, but there is no requirement that the names of
forms be unique in a document.

Incidentally, the W3C HTML validator is happy for A elements to have
the same name. Perhaps that's an issue with the DTD or that
particular validator.

I guess the other statement you are referring to is in regard to "W3C
guidelines", I can't comment on that because I don't know enough about
what the W3C may have posted as "guidelines". Maybe Erwin has a
quote?
 
E

Erwin Moller

RobG said:
I'm not sure what you mean by "both statements". I stand corrected in
regard to A elements, but there is no requirement that the names of
forms be unique in a document.

Incidentally, the W3C HTML validator is happy for A elements to have
the same name. Perhaps that's an issue with the DTD or that
particular validator.

I guess the other statement you are referring to is in regard to "W3C
guidelines", I can't comment on that because I don't know enough about
what the W3C may have posted as "guidelines". Maybe Erwin has a
quote?

Hi Rob,

No I don't, hence my formulation:
"I am not sure, but I expect all forms should have different names
according to W3C guidelines, so there is another reason to rename them."

I think giving different forms the same name in a document is 'bad
coding', but that is all.
Arrogant as I am, I expected that my wishes would have been written down
somewhere by some very knowledgable people.
;-)

Seriously, if you need a form and use the name, like:
document.forms["aname"]
, it would come in really handy if they have different names.

By the way: What would JavaScript return if 3 forms exists with that name?
An Array containing them?
The first one?
Last one?
Well, I could try that myself of course, but I won't since I'll never
ever be in the situation I name my forms the same. ;-)

Regards,
Erwin Moller
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top