Javascript error

Discussion in 'JavaScript and AJAX' started by Soutrik, Jul 11, 2009.

  1. Soutrik

    Soutrik New Member

    Joined:
    Jul 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Project Researcher
    I am using this code in a HTML page but every time the result is erroneous.


    function check()
    {var a=document.getElementById("txt_user").value.trim;
    var b=document.getElementById("txt_pwd").value.trim;
    if((a=="")||(b==""))
    {alert("Required fields can't be left empty");
    return false;
    }
    else if(a==b)
    {alert("Username and password are same");
    return false;
    }
    }
    Whatever be the values in textboxes txt_user and txt_pwd the alert comes as "Username and password are same".
    Please let me know if there is any bug in the code.
    Thanks in advance.
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    OMG ... It's all about a pair of parentheses () !!
    Even I was surprised at first sight, how can that code give some errors ?!

    Finally found it, though. Here is the rectified code :

    Code:
    function check()
    {
        var a=document.getElementById("txt_user").value.trim();
        var b=document.getElementById("txt_pwd").value.trim();
        if((a=="")||(b==""))
        {
            alert("Required fields can't be left empty");
            return false;
        }
        else if(a==b)
        {
            alert("Username and password are same");
            return false;
        }
    }
    You missed a pair of parentheses behind the trim function. :)
     
  3. Soutrik

    Soutrik New Member

    Joined:
    Jul 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Project Researcher
    Thanks Sir...
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice