Validating a Form With JavaScript

JavaScript is an object-oriented, interpretive language used with HTML to provide dynamic capabilities to otherwise static webpages.

A JavaScript function included in an HTML page is processed and executed by the web browser. Integrating a JavaScript function into an HTML page that calls WebFOCUS, or a page that displays a report, enables you to:

You have many additional design capabilities with JavaScript. See your JavaScript documentation for details on JavaScript techniques and syntax.

Example: Checking For a User-Supplied Value on a Form

Note: For information on where to store the files created in this example, see Defining and Allocating WebFOCUS Files.

  1. Create a launch page named VALIDATE, which calls a JavaScript function named checkInput. This procedure must be accessible to the web server.

    The sample launch page uses the Servlet.

    Launch Page: VALIDATE.HTM

    <HTML>
    <HEAD>
    <TITLE> WebFOCUS Report </TITLE> 
    <SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
    function checkInput() {
      if(document.form2.STATE.value == "") {
        alert("You must enter a value for the state!");
        return false;
      }
        else {
          return true;
        }
      document.form2.submit();
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <H4 ALIGN=CENTER>State Inventory Report</H4>
    <HR>
    <FORM ACTION="/ibi_apps/WFServlet" METHOD="get"
    TARGET="_blank" NAME="form2" onSubmit="return checkInput()">
    <INPUT NAME="IBIF_ex" VALUE="stsales" TYPE="hidden">
    <P ALIGN=LEFT NOWRAP><PRE>
    <B>Enter a state (for example, CA or NY): </B><INPUT NAME="STATE" 
    TYPE="text" SIZE="2">
    </PRE></P>
    <P><PRE></PRE></P>
    <P>
    <INPUT NAME="submit" TYPE=SUBMIT VALUE="Run Report"> 
    <INPUT NAME="reset" TYPE=RESET VALUE="Clear Form">
    </P>
    </FORM>
    </BODY>
    </HTML>
  2. Create a procedure named STSALES, which generates an inventory report for eMart, in the state the user requests.

    Procedure: STSALES.FEX

    TABLE FILE CENTORD
    SUM QTY_IN_STOCK BY STATE BY STORENAME BY PRODNAME
    WHERE STATE EQ '&STATE'
    WHERE STORENAME EQ 'eMart'
    END
  3. Run the launch page.


  4. Without entering a value for the state, click Run Report. The following message displays: