Help: Object Expected error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TAM

    #1

    Help: Object Expected error

    Hi,

    I have a simple JavaScript code that ensures that all the form fields
    are filled and there is also a function that checks if the email is a
    valid address. For some reason IE is giving "Object Expected" error on
    the alert statement. The same code doesn't give an error in NN or
    Opera or Mozilla or FireFox.

    Can someone give me a clue as what could be wrong here?

    Thanks,

    joe

    <script language = "JavaScript ">
    function validate(){
    if ((document.Solu tionEval.pwd.va lue ==
    "")||(document. SolutionEval.pw d.value == null)){
    alert("Please enter correct password")
    document.Soluti onEval.pwd.focu s()
    return false;
    }
    if (document.Solut ionEval.name.va lue == ""){
    alert("Please enter your name")
    document.Soluti onEval.name.foc us()
    return false;
    }
    if (document.Solut ionEval.company .value == ""){
    alert("Please enter your company name")
    document.Soluti onEval.company. focus()
    return false;
    }
    if (document.Solut ionEval.phone.v alue == ""){
    alert("Please enter your phone number")
    document.Soluti onEval.phone.fo cus()
    return false;
    }
    if ((document.Solu tionEval.email. value ==
    "")||(document. SolutionEval.em ail.value == null)){
    alert("Please enter your email address")
    document.Soluti onEval.email.fo cus()
    return false;
    }
    /* check if the email address is valid */
    if (echeck(documen t.SolutionEval. email.value)==f alse){
    document.Soluti onEval.email.va lue=""
    document.Soluti onEval.email.fo cus()
    return false;
    }
    return true;
    }

    function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf (at)
    var lstr=str.length
    var ldot=str.indexO f(dot)
    if (str.indexOf(at )==-1){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.indexOf(at )==-1 || str.indexOf(at) ==0 ||
    str.indexOf(at) ==lstr){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.indexOf(do t)==-1 || str.indexOf(dot )==0 ||
    str.indexOf(dot )==lstr){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.indexOf(at ,(lat+1))!=-1){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.substring( lat-1,lat)==dot ||
    str.substring(l at+1,lat+2)==do t){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.indexOf(do t,(lat+2))==-1){
    alert("Invalid E-mail ID")
    return false
    }

    if (str.indexOf(" ")!=-1){
    alert("Invalid E-mail ID") << This where IE gives error "Object
    Expected"
    return false
    }

    return true
    }
    </script>
  • Michael Winter

    #2
    Re: Help: Object Expected error

    On 19 Aug 2004 11:15:56 -0700, TAM <green2004@cana da.com> wrote:
    [color=blue]
    > I have a simple JavaScript code that ensures that all the form fields
    > are filled and there is also a function that checks if the email is a
    > valid address. For some reason IE is giving "Object Expected" error on
    > the alert statement. The same code doesn't give an error in NN or
    > Opera or Mozilla or FireFox.
    >
    > Can someone give me a clue as what could be wrong here?[/color]

    I can't see any problems with your code per se, but there are some
    improvements to be made. The e-mail address validation, for instance, can
    be greatly simplified with a regular expression.
    [color=blue]
    > <script language = "JavaScript ">[/color]

    Valid HTML requires the type attribute. The language attribute is
    deprecated and shouldn't be used any more.

    <script type="text/javascript">
    [color=blue]
    > function validate(){[/color]

    The first thing you could do here is save a reference to the form,
    specifically, it's elements collection. It will save you having to type
    out 'document.Solut ionEval.' every time:

    var elem = document.forms['SolutionEval'].elements;

    To access the password control below, you would write:

    elem['pwd'].value
    [color=blue]
    > if ((document.Solu tionEval.pwd.va lue ==
    > "")||(document. SolutionEval.pw d.value == null)){[/color]

    The value property will *never* be null. Testing for that condition is
    pointless. Also, you can evaluate a string as a boolean (true/false). If
    the string is empty (length zero), the expression will be false (see
    below).
    [color=blue]
    > alert("Please enter correct password")
    > document.Soluti onEval.pwd.focu s()
    > return false;[/color]

    Based on all of that, you could write the above as:

    /* The ! (logical NOT) operator evaluates an expression
    * as a boolean, then reverses the result.
    *
    * if(elem['pwd'].value) {
    * // pwd contains at least one character
    * } else {
    * // pwd is empty
    * }
    *
    * Add ! to the if expression (as I do below), and the first
    * block will be executed if pwd is empty.
    */
    if(!elem['pwd'].value) {
    alert('Please enter a password.');
    elem['pwd'].focus();
    return false;
    }

    You can apply the same pattern to all of the other checks.

    [snip]

    As I said earlier, you can check an e-mail address with a regular
    expression. The one below, taken from Dr Stockton's validation page is
    greatly simplified, and will allow some invalid addresses, but it does
    ensure basic form.

    /^.+@.+\..+$/

    You could change the last block in your validate function to:

    if(!/^.+@.+\..+$/.test(elem['email'].value)) {
    alert('Please enter a valid address.');
    elem['email'].focus();
    return false;
    }

    and delete echeck().

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    Working...