• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Unable to validate form using javascript

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi try,

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

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic