Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Expected an object' error

Status
Not open for further replies.

Gaelle

Programmer
Jun 20, 2001
62
0
0
FR
Hello !

I have been working with Javascript for long now, and I have a problem I can't solve (driving me insane).
I wrote a function to check the fields of a form befor submitting it.It goes like this :

function verif() {
var b1 = (formRech.dCodeSCS.value=="");
var b2 = (formRech.dNTitulaire.value=="");
if ((not(b1)) and (not(b2))
{alert("You can't fill both options");}
else {if (b1 and b2)
{alert("Please fill in a field");}
else {if (b1) {formRech.SFRou7.value="7";formRech.submit();}
else {document.formRech.SFRou7.value="SFR";formRech.submit();}
}
}
}

Hope it can be understood !
And then I call the function like this:

<a href=&quot;#&quot;><img src=&quot;images/valider.gif&quot; onClick=&quot;verif();&quot; width=&quot;78&quot; height=&quot;26&quot; border=&quot;0&quot; alt=&quot;Lancer la recherche&quot;></a>

When I click on the image, ie gives me an error : Object expected (in French : objet attendu), and I just CAN'T understand why it won't work !

Please help me!!!

Gaelle, from France.
 
You have one too many end parentheses in your [COLOR=aa0000]
Code:
if
[/color] statement. This causes a syntax error and keeps the function from being parsed, hence rendering the function nonexistant and causing an &quot;Object Expected&quot; error when you try to call it.

For some reason, IE by default will not show an error message for unterminated string constants and syntax errors. If you check the box in the error report saying &quot;Always display this message when a page contains errors,&quot; then it will show the error message for every error you come across (including syntax errors).
Code:
- UNIMENT
 
&quot;not&quot; and &quot;and&quot; are NOT Javascript

if (!b1 && !b2)
{alert(&quot;You can't fill both options&quot;);}
else if (b1 && b2)
{alert(&quot;Please fill in a field&quot;);}
else if (b1)
{
formRech.SFRou7.value=&quot;7&quot;;
formRech.submit();
}
else
{
document.formRech.SFRou7.value=&quot;SFR&quot;)
formRech.submit();
}
 
Gaelle,

Tu as l'air d'avoir de l'experience avec JavaScript mais il faudrait que tu ameliores la lisibilite de ton code. L'utilisation d'une convention est toujours une bonne idee. Essaye d'utiliser ce format (convention ):

function monFormulaireEstValide()
{
if (document.monFormulaire.asdf.value != &quot;valSouhaite&quot;)
{
alert(&quot;pas la bonne valeur&quot;)
return false;
}
return true
}

regarde l'utilisation des minuscules et des majuscules. Et regarde la lisibilite en espacant les accolades.

tu pourrais par exemple utiliser cette fonction de cette maniere :

<a href=&quot;javascript:document.monFormulaire.submit()&quot; onclick=&quot;return monFormulaireEstValide();&quot;><img></a>

si monFormulaireEstValide() renvoi false le href ne sera pas lance.

Aussi si tu ne fait pas le submit de ton formulaire par du javascript mais par un bouton submit normal tu peux utiliser cette methode :

<form name=monFormulaire onsubmit=&quot;return monFormulaireEstValide()&quot;>

Bonne journee.
Gary &quot; Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top