Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to validate form using javascript

 
Praneet Mirchandani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am unable to validate my form using javascript. Here's my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index Page</title>
<script type="text/javascript">
function check(){
if(username.value == "" || password.value == ""){
alert("Username or password cannot be blank");
//elem.focus(); // set the focus to this input
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="welcome.jsp" >
UserName: <input type="text" name="username">
Password: <input type="password" name="password">



<input type="submit" name="submit" value="Submit" onclick="check()">
<input type="reset" name="reset">
</form>
</body>
</html>


Any idea what's wrong??

Praneet
 
Ad Rajesh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi try,

document.forms[0].username.value and the same for password.
It should work.

 
Mahendran Aiyappan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have some thing like this:

<input type="text" name="username" id="username">
<input type="password" name="password" id="password">


and then in the script function you can have

if(document.getElementById("username").value == "" || document.getElementById("password").value == ""){
alert("Username or password cannot be blank");
//elem.focus(); // set the focus to this input
return false;
}

Mahendran.
 
Praneet Mirchandani
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey

thanks a lot....that worked...
both worked on an editor i tried online...but in eclipse only your suggestion worked....
maybe its got to do with weblogic server...have to use it....not allowed to use apache...

cheers
Praneet
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

The server has absolutely nothing to do with JavaScript: JavaScript runs on the *client*--the browser.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the server.

You are not returning false in the onclick event itself. You actually should be using onsubmit on the form and not with an onclick event.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic