0

I have written a simple Javascript function as follows:

function loginValidate(){
   var rValue = true;
   var username = document.getElementById("username").value; 
   var password = document.getElementById("password").value;

   if(username=="" || password==""){
      alert('Empty field');
      rValue = false;
   }
   return rValue;
   //alert('hi');
}

When I hit the submit button I am getting the error as: Object required : loginValidate.js line 3 character 3

I am getting the error on both IE 8 and FF 10 but it works fine in Eclipse internal browser.

Am I missing something? Kindly help, thanks in advance.

1
  • document.getElementById got nothing, so cannot have .value Commented Feb 13, 2012 at 5:31

2 Answers 2

2

I'm guessing that when that function runs, document.getElementById("username").value; is not finding an element. As a result document.getElementById("username") returns null and null.value throws an exception, since null is not allowed to have properties.

Exactly why that is not finding your element depends on your HTML and when this JS snippets runs.

Sign up to request clarification or add additional context in comments.

Comments

1

try to put this text to debug

alert(document.getElementById("username")); alert(document.getElementById("password"));

if it said htmlxxxelement, then it found something. where xxx is like div, table, or input if it said undefined, or null, then it doesnt found the HTML control. you need to check the spelling. or create the control somewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.