aspose file tools*
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 Java 8 in Action this week in the Java 8 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to validate form using javascript
 
Similar Threads
how to get input from javascript and pass to servlet
error in servlet due to reasons unknown
JSP not calling JS file funtion
JSP with DWR.XML
submit button is not working