ASM wrote on 09 dec 2006 in comp.lang.javas cript:
JV a écrit :
>I thought I once saw somewhere that a global variable, "xyz" for
example, could be declared and used so that instead of using
"document.form name.elementnam e.value" one could use
"xyz.elementna me.value" or use "xyz" in place of "document.formn ame"
anytime a reference to a form element was made.
that shortcut is browser specific, I heard.
>Is this actually true or not? I'm going crazy trying to recall
something that might not even exist!
function verif() {
var f = document.forms['myForm']; // shortcut for form named 'myForm'
shouldn't that be for completeness:
var f = document.forms['myForm'].elements
[..]
=============== ==========
Stipulating that you use the return false/true from an onsubmit='..',
"this" is a good alternative, making the function universal:
<form onsubmit='verif (this)' ...
....
function verif(fm) {
var f = fm.elements
for(var i=0; i<f.length; i++)
if(f[i].type == 'text' && f[i].value=='') {
alert('field "'+f[i].name+'" not filled');
f[i].focus();
f[i].select();
return false;
}
return true;
}
=============== ==========
What about this:
function verif(fm) {
var f = fm.elements;
i=0;
while (i<f.length && (f[i].type!='text' || f[i].value!=''))
i++;
if (i==f.length) return true;
alert('field "'+f[i].name+'" not filled');
f[i].focus();
f[i].select();
return false;
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)