DW function, Firefox works, IE doesn't.

F

Fabri

I would like to ask you the following:

I use Macromedia Dreamweaver as an editor for HTML and Js.

It also writes some js functions to simply validate forms.

I always used it with no bugs.

Till now. This is the problem (and the page):

===========================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

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 == '');
}
//-->
</script>
</head>
<body>
<form name="formA">
<input type="text" name="pippo">
<input type="text" name="email">
<input name="A" type="submit"
onClick="MM_validateForm('pippo','','R','email','','RisEmail');return
document.MM_returnValue" value="Invia">
</form>

<form name="formB">
<input type="hidden" name="pippo">
<input type="hidden" name="email">
</form>

</body>
</html>

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

My target is to validate "pippo" to have it not empty and "email" not
empty but also a valid email.

Dreamweaver wrote these 2 functions to do this and added onClick event
to button this way.

The only new thing I did is to write an hidden form with same fields
names because I have to do this before posting data.

I tried this code in Firefox and it works fine.

I tried this in IE and it says: "'val' is null or it is not an object".

What do you think about it?

Any help much appreciated.

Regards.
 
R

RobB

Fabri said:
I would like to ask you the following:

I use Macromedia Dreamweaver as an editor for HTML and Js.

It also writes some js functions to simply validate forms.

I always used it with no bugs.

Till now. This is the problem (and the page):

===========================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

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 == '');
}
//-->
</script>
</head>
<body>
<form name="formA">
<input type="text" name="pippo">
<input type="text" name="email">
<input name="A" type="submit"
onClick="MM_validateForm('pippo','','R','email','','RisEmail');return
document.MM_returnValue" value="Invia">
</form>

<form name="formB">
<input type="hidden" name="pippo">
<input type="hidden" name="email">
</form>

</body>
</html>

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

My target is to validate "pippo" to have it not empty and "email" not
empty but also a valid email.

Dreamweaver wrote these 2 functions to do this and added onClick event
to button this way.

The only new thing I did is to write an hidden form with same fields
names because I have to do this before posting data.

I tried this code in Firefox and it works fine.

I tried this in IE and it says: "'val' is null or it is not an object".

What do you think about it?

Any help much appreciated.

Regards.


Well...you've got two pippos now so, you're up the creek. ;)

That MickeyMouse_findObj() routine isn't very good with same-named
form elements, which IE puts in an array (inside the document.all[]
array) so it can store pointers to both. Couldn't figure out any way
to pass that reference - pippo[0] - as a string argument, so the best
fix is probably to lose that function completely, and change this
line:

val=MM_findObj(args);

....to

val=document.getElementById(args);

Then:

<form name="formA">
<input id="pippo1" type="text" name="pippo" value="">
<input id="email1" type="text" name="email" value="">
<input name="A" type="submit"
onClick="MM_validateForm('pippo1','','R','email1','','RisEmail');return
document.MM_returnValue" value="Invia">
</form>

<form name="formB">
<input id="pippo2" type="hidden" name="pippo">
<input id="email2" type="hidden" name="email">
</form>

....and it should work. Totally useless (catch-all) code for simple
validations, though.

Always set value="" for form elements, some browsers will do it it for
you otherwise.
 
F

Fabri

RobB said:
val=MM_findObj(args);

...to

val=document.getElementById(args);
[CUT]

...and it should work. Totally useless (catch-all) code for simple
validations, though.



Rob...simply perfect :)

Thank you very much.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top