Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   Javascript error (http://www.go4expert.com/forums/javascript-error-t18486/)

Soutrik 11Jul2009 23:36

Javascript error
 
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.

SaswatPadhi 26Jul2009 07:57

Re: Javascript error
 
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: JavaScript

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. :)

Soutrik 6Sep2009 23:11

Re: Javascript error
 
Thanks Sir...

SaswatPadhi 7Sep2009 12:51

Re: Javascript error
 
My pleasure :)


All times are GMT +5.5. The time now is 03:33.