开发者

Javascript Validation Syntax Errors

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 e开发者_StackOverflow社区rror 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,'');
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜