0
function validate_login(form){
 alert(form);
 var formName = form.name;
 alert(formName);

 if(!validate_username(form)){
    return false;
 } else{
  return validate_password(form);
 }
}

i called the above function in an external javascript as

<form action="LoginServlet" method="post" class="message" id="login_form" name ="login_form" onsubmit="javascript:return validate_login(this.form)">

but alert(form) shows an alert having output 'undefined' and alert(fornName) is not showing an alert. help me pleasee..

5
  • "javascript:" is useless Commented Nov 13, 2014 at 13:48
  • this refers to the form. this.form is trying to access the form property on the form... probably not what you're after Commented Nov 13, 2014 at 13:49
  • The second alert cannot show, because you try to access the property name of the undefined object form Commented Nov 13, 2014 at 13:50
  • 1
    Side note: if/else can be replaced by return validate_username(form) && validate_password(form);. Commented Nov 13, 2014 at 13:50
  • For debugging purposes it is better to use console.log or a Javascript Debugger like the Chrome Developer Tools! Commented Nov 13, 2014 at 13:51

2 Answers 2

1

You're not passing the form correctly, pass only this. What you're trying to do is passing the property form of the form.

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

Comments

1

Change

onsubmit="javascript:return validate_login(this.form)"

to

onsubmit="return validate_login(this)"

Running Example

function validate_login(form) {
  alert(form);
  var formName = form.name;
  alert(formName);

  return false;
}
<form action="LoginServlet" method="post" class="message" id="login_form" name="login_form" onsubmit="return validate_login(this)">
  <input type="submit" />
</form>

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.