Embedding JavaScript in JSP

Posted on 2004-11-19
Last Modified: 2008-01-09
The problem is I've created a JSP page and I would like to be able to click on a button...which will lead to a JavaScript function that will validate form information here is the code:

The JSP file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Untitled Document</title>
<SCRIPT LANGUAGE="JavaScript" SRC="myscript.js">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<jsp:include page="/banner.html"/>
<%@page import="finalProject.beans.*, java.util.*,finalProject.*,java.text.*" %>


<form name="customerForm" action="/FinalProject/servlet/finalProject.registrationServlet" method="post">
                  <select name="prefix">
                        <option value="" selected>--</option>
                        <option value="Ms">Ms</option>
                        <option value="Mr.">Mr.</option>
                        <option value="Mrs.">Mrs.</option>
                        <option value="Miss">Miss</option>
            <tr><td>Enter Your First Name: <br> <input name="first" value="" type="text"></td>
            <tr><td>Enter Your Last Name:<br><input name="last" value="" type="text"></td></tr>
            <tr><td>Enter Your Telephone Number:<br><input name="phone" value="" type="text"></td></tr>
            <tr><td>Enter a Street Address:<br><input name="add" value="" type="text"></td><td>City:<br><input name="city" value="" type="text"></td><td>State:<br><input name="State" value="" type="text"></td><td>Zip:<br><input name="zip" value="" type="text"></td></tr>
            <tr><td>Enter Your Email Address:<br><input name="email" value="" type="text"></td></tr>
      <h3>Please Select a User Name and Password for your account:</h3>
      <tr><td>UserName:<br><input type="text" value="" name="user"></td><td>Password:<br><input type="password" value="" name="password"></td></tr>
      <h3>Please Enter Your Billing Information:</h3>
      <tr><td>Credit Card Type<br>
            <select name="credittype">
                  <option value="" selected>--</option>
                  <option value="Visa">Visa</option>
                  <option value="MasterCard">MasterCard</option>
                  <option value="Amex">American Express</option>
                  <option value="Discover">Discover</option>
      </td><td>Card Number:<br><input type="password" value="" name="creditNum"></td></tr>
<center><input name="submitIt" value="Register Me!" type="button"></center>

The JS File:
// JavaScript Document
function validateForm(form){
alert("In validateForm");
if(form.prefix.value==null || form.prefix.value==""){
      alert("Please select a Prefix!");;
else if(form.first.value==null || form.first.value==""){
      alert("Please Enter Your First Name!");;
else if(form.last.value==null || form.last.value==""){
      alert("Please Enter Your Last Name");;
else if( ||""){
      alert("Please your Phone Number!");;;
else if(form.add.value==null || form.add.value==""){
      alert("Please Enter Your Address!");;
else if( ||""){
      alert("Please Enter a City!");;;
else if(form.State.value==null || form.State.value==""){
      alert("Please Enter a State!");;
if( ||""){
      alert("Please Enter your Zip Code!");;;
if( ||""){
      alert("Please Enter an Email!");;;
if(form.user.value==null || form.user.value==""){
      alert("Please Enter a User Name!");;
if(form.password.value==null || form.password.value==""){
      alert("Please Enter a password!");;
if(form.credittype.value==null || form.credittype.value==""){
      alert("Please Select a Credit Card Type!");;
if(form.creditNum.value==null || form.creditNum.value==""){
      alert("Please Enter Your Credit Card Number");;


}//close function

I click on the button and NOTHING happens....any ideas.  Is this even possible?
Question by:cbshell
    1 Comment
    LVL 2

    Accepted Solution

    Look like you've got a few small problems.


    <input name="submitIt" value="Register Me!" type="button">

    should be

    <input name="submitIt" value="Register Me!" type="submit">


    <form name="customerForm" action="/FinalProject/servlet/finalProject.registrationServlet" method="post">

    should be

    <form name="customerForm" action="/FinalProject/servlet/finalProject.registrationServlet" method="post" onSubmit="return validateForm(this);">


    all of the

    return; in the javascript

    should be

    return false;

    That should do it.

    Hope that helps.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    DECT technology has become a popular standard for wireless voice communication. DECT devices are not likely to be affected by other electronic devices and signals because they operate in a separate frequency-band.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now