Generating JavaScript Code

Example 17-6 shows a modified version of the User Info page used in the examples in Chapter 10.

Example 17-6. Input form with client-side validation code (clientscript.jsp)
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/core" %>
  
<html>
  <head>
    <title>User Info Entry Form</title>
    <script language="JavaScript"> <!-- Hide from browsers without JavaScript support function isValidForm(theForm) { if (isEmpty(theForm.userName.value)) { theForm.userName.focus( ); return false; } if (!isValidDate(theForm.birthDate.value)) { theForm.birthDate.focus( ); return false; } if (!isValidEmailAddr(theForm.emailAddr.value)) { theForm.emailAddr.focus( ); return false; } if (!isValidNumber(theForm.luckyNumber.value, 1, 100)) { theForm.luckyNumber.focus( ); return false; } return true; } function isEmpty(aStr) { if (aStr.length == 0) { alert("Mandatory field is empty"); return true; } return false; } function isValidDate(dateStr) { var matchArray = dateStr.match(/^[0-9]+-[0-1][0-9]-[0-3][0-9]$/) if (matchArray == null) { alert("Invalid date: " + dateStr); return false; } return true; } function isValidEmailAddr(emailStr) { var matchArray = emailStr.match(/^(.+)@(.+)\.(.+)$/) if (matchArray == null) { alert("Invalid email address: " + emailStr); return false; } return true; } function isValidNumber(numbStr, start, stop) { var matchArray = numbStr.match(/^[0-9]+$/) if ...

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.