
JavaScript is mostly used scripting language in web world. All the browsers support JavaScript which makes JavaScript more popular among all categories of web programmers. Now we will look how do we use the JavaScript with the JSF application to validate out registration form. The following are the steps of including the JavaScript in the JSF application.
Step 1:-Write the validate function in the JavaScript using the <script> tag. We are putting the following checks in the registration.jsp page.
- All fields are required. It means no field accept the blank. We need to supply the value for each field.
- The password and retype password should match.
Note:- In JSF the form name is appended with the name of the fields when the HTML is rendered on the browsers. So for validation one need to use the form name and a colon(:) with the field name. For example, if we have password for the form1, then in JavaScript we need to use form1:password to get the object.
Step 2:- Calling the validate function on the onsubmit event of the form.
We need to call the validate method in order to validate the user specified values in the JSF. The code for this is :-
<h:form id="form1" onsubmit="return validate();">
The following is the code for Registration.jsp after adding the JavaScript's validate method.
<%@page contentType="text/html"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <%-- <script> <html> </h:form> |
Note:- We can also use the server side validation by writing some methods in the backing beans. This we will learn in coming sections.
Jsp Discussion
- - Two forms in one JSP
- - PASS VARIABLES BETWEEN 2
- - Table data
- - Unable to laod the image
- - The Ultimate Web UI Frame




