This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Unable to validate form using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Unable to validate form using javascript" Watch "Unable to validate form using javascript" New topic
Author

Unable to validate form using javascript

Praneet Mirchandani
Ranch Hand

Joined: Dec 16, 2009
Posts: 46
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


Thanks,
Praneet
Ad Rajesh
Greenhorn

Joined: Apr 25, 2006
Posts: 21
hi try,

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


SCJP 1.4
SCWCD 5.0
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
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

Joined: Dec 16, 2009
Posts: 46
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
 
 
subject: Unable to validate form using javascript