• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Help with form validation code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im trying to validate a form on a jsp page with java script. The user is supposed to enter text in to 3 text fields and I want a pop up to come up if they leave a field blank. Ill post my code..can someone give me some tips and improvements?

Also, two of the fields are supposed to be dates (day of the month and year). Is there any way to write an alert if the user doesnt enter a number between 1-31 for the date or some crazy year for the year?

Sorry if Im not making sense. If you need clarification please ask!

Thanks guys!

Here is the code

<form action="birthTrivia" method="POST">

<% User user = (User) request.getAttribute("user"); %>

<script language="JavaScript">
function validate(form){
if (form.Name.value=="")
{alert("Please fill in your name!");
form.Name.focus();
}
else if (form.Day.value=="")
{
alert("Please enter a day!");
form.Day.focus();
}
else if (form.Year.value=="")
{
alert("Please enter a year!");
form.Year.focus();
}
else
{
form.submit();
}
}
</script>

Please enter your name: <input type="text" name="Name" value="<%= user.getName() %>)" /><br>
Please enter the day you were born (In XX form): <input type="text" name="Day" value="<%= user.getDay() %>" /><br>
Please select the month in which you were born: <select name="month">
<option>January</option>
<option>Febuary</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select><br>
Please enter the year you were born (In 19XX form): <input type="text" name="Year" value="<%= user.getYear() %>" /><br>

<br><input type="button" value="Submit" <onClick<="validate(this.form)" /> <input type="reset" value="Reset" />



</form>
[ December 08, 2008: Message edited by: Douglas Klein ]
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What problems are you having?
[ December 08, 2008: Message edited by: Bear Bibeault ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to use the JavaScript Object:
http://www.javascriptkit.com/jsref/date.shtml

Eric
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic