Forum Moderators: open

Message Too Old, No Replies

Need help on my own code....

GRR, error on a form/javascript and I can't find it

         

mikejson

7:04 pm on Oct 16, 2003 (gmt 0)

10+ Year Member



<script language="javascript">
function validateForm( form ) {
if( getRadioValue( form.FCONTACT ) == null ) {
alert("Please choose one answer before hitting submit");
return false ;
}
else {
alert("THANK YOU!");
return true ;
}
}

function getRadioValue( radio ) {
var i;
for( i = 0; i < radio.length; i++ ) {
if( radio[i].checked )
return radio[i].value;
}
return null;
}
</script>
<form name="PRSURVEY" OnSubmit="return validateForm( form )"

That's the script, and that's the tag of the form, up to what should matter....

What is going on, I keep getting an error(javascript error I believe) but since it's a form, I can't view the error, cause it loads the next page right away. The problem is the validate function isn't working. I'm validating that a radio button has been selected FCONTACT is the name of the radio button group. I can't influence the selection by automatically loading the page to select one, so I wrote a little function to check and see what was selected and if 1 was, return the value, if it wasn't return null...help...

*edit*
ewww, formatting of my code doesn't look that sloppy hehe...

garann

5:58 pm on Oct 17, 2003 (gmt 0)

10+ Year Member



Could it be this?
OnSubmit="return validateForm[b]( form )"[/b] 

My guess is that changing ( form ) to ( this ) or ( document.forms[0] ) or something else that denotes an element in your code would fix this. In the code you have, I'd expect "form" to be the name of a JavaScript variable or the ID of your form. It doesn't seem to be either, which would mean you're passing a null value to your function.

hth,
g.

DrDoc

6:16 pm on Oct 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...or this.form

And then don't use "form" as a variable in your script. Use something else, like myForm instead

kaled

11:22 am on Oct 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Standard programming logic - if the submit action of the form makes debugging the code difficult, call the code using another button, link, whatever so you can see what's going on.

Also, whether using IE, Opera or whatever, you should ensure that javascript errors are reported.

Kaled.

MonkeeSage

7:26 pm on Oct 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JS's
try {... } catch() {}
statement can also be your best friend for hard-to-view errors...like...

... 
// good code
try {
// evil, maleficent code spawned
// from the flames of tartarus
}
catch(e) {
alert(e);
}
...

Then you'll get a nice alert telling you the problem...like...'your code has taken to living in graveyards and sucking the blood of small rodents and flightless fowl; we think it's possessed' ;)

Jordan

korkus2000

3:00 am on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jordan, do you know what browsers support try catch? I have used it in windows programming but never knew that JS had it in the language.

MonkeeSage

8:55 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had just been assuming it worked in all the browsers, but never tried it. From a quick test it looks like that is pretty much correct...

javascript:try { alert(outofscopevar); } catch(e) { alert(e); }

In IE, though, it only alerts the object type "[object error]" and I'm not sure which attribute needs to be addressed to get the actual error message it shows with Opera / Mozilla.

Jordan

MonkeeSage

8:56 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops...double-post...delete me