views:

39

answers:

2

Hi guys,

I have this piece of javascript which is supposed to validate on the clientside. I created it using this tutorial: http://www.php-mysql-tutorial.com/wikis/php-tutorial/form-validation-using-php.aspx

Unfortunately I'm getting this warning: "There is a syntax error on line 48. Code hinting may not work until you fix this error".

My code is 110 lines long, so I didn't want to post it here so I ran this code through http://www.jslint.com/, and this is what that told me: "Problem at line 4 character 5: Expected an identifier and instead saw 'with'."

I'm still fairly lost, so here is a snippet from the start of the code of the code:

function checkForm()
{
    var vcompName, vadd1, vadd2, vcountry, vcontact1, vtelephone1, vemail, vsiteurl;
    with(window.document.form1)
    {
        vcompName = compName;
        vadd1 = add;
        vadd2 = add2;
        vcountry = country;
        vcontact1 = name;
        vtelephone1 = tel;      
        vemail = email1;
        vpackage = package;
        vsiteurl = url;
    }

    if(trim(vcompName.value)=='')
    {
        alert('Please enter the company name');
        vcompName.focus();
        return false;
    }
    else if(trim(vadd1.value)=='')
    {
        alert('Please enter your address')
        vadd1.focus();
        return false;
    }
}

You can see how it goes... Here is the trim function

function trim(str)
{
    return str.replace(/^\s+|\s+$/g,'');
}
+1  A: 

Not a real answer to neither of your questions, but to clarify: The two errors have nothing to do with each other.

The former seems to be your IDE not understanding the regular expression - not sure what can be done about that. Which IDE are you using?

The latter may be JSLint rejecting the with keyword - not sure why though, it still seems valid syntax.

Pekka
Hi Pekka, I am using dreamweaver CS4. I still went ahead and tested the script (in hope!!) and it didn't work.
TaraWalsh
@Tara what error message did you get from the browser when you tested it?
Pekka
@Pekka, I didn't get any error at all. I didn't enter in a value for one of the text boxes and it just didn't catch that.
TaraWalsh
Presumably jslint rejects `with` because it's largely regarded as a foul horror that should never be used (it's flat-out invalid in ECMAScript Fifth Edition's ‘strict mode’). The whole tutorial's a bit crap really, with the silly suggestion to strip tags, and the utterly misguided `isEmail` check. (Is it this regex rather than the trim that's causing the problem? It's certainly way uglier. And really, still trying to check for “valid” TLDs, in this day and age? Have we learned nothing?)
bobince
@bobince: You seem to know your stuff! Thanks for the advice. Is there any other tutorials you can recommend?
TaraWalsh
+1  A: 

As such syntax is alright but you should avoid with keyword as its slow and there can be ambiguity. I will suggest you to rewrite the below block

with(window.document.form1)
    {
        vcompName = compName;
        vadd1 = add;
        vadd2 = add2;
        vcountry = country;
        vcontact1 = name;
        vtelephone1 = tel;      
        vemail = email1;
        vpackage = package;
        vsiteurl = url;
    }

as

  var o = window.document.form1;
   vcompName = o.compName;
   vadd1 = o.add;
   vadd2 = o.add2;
   vcountry = o.country;
   vcontact1 = o.name;
   vtelephone1 = o.tel;      
   vemail = o.email1;
   vpackage = o.package;
   vsiteurl = o.url;

There are few other issues but jlint would point them to you.

VinayC