VisualBuilder
  Home > Jsp > Tutorials > Using Javascript to Validate Form - JSF Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
JSF Tutorial Home
1 . Introduction to JSF
2 . Life Cycle of JSF Application
3 . JSF Releases
4 . Comparing JSF With Existing Web Technologies
5 . Steps To Create JSF Application Using NetBeans IDE
6 . Web.xml file
7 . First Hello World Application
8 . Running the Application
9 . Introduction To JSF Tag Libraries
10 . Form Designing With JSF
11 . Using Managed Bean With JSF -1
12 . Using Managed Bean With JSF -2
13 . Using Javascript to Validate Form
14 . Adding Navigation in JSF Application
15 . Creating the Reponse page for Registration Application
16 . Conversion Model of JSF
17 . Example Continued For Conversion Model
18 . Displaying the Data in Data Table
19 . Example Continued For Data Table
 
 
JSP Home
JSP Members (18709)
JSP Member Articles ( 11 )
JSP Discussion (71)
JSP Q & A ( 87 )
- JSP Ask Question
- JSP Questions
- JSP Unanswered Questions
JSP Resources
JSP Source Code (172)
JSP Articles (34)
JSP Blogs (333)
JSP Jobs (0)
JSP Components (10)
JSP Books (9)
JSP Websites (13)
JSP News (12)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Jsp jsf Tutorial
 Using Javascript to Validate Form
  << Prev: Using Managed Bean With JSF -2 Next: Adding Navigation in JSF Application >>

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"%>
<%@page
pageEncoding="UTF-8"%>


<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<%--
This file is an entry point for JavaServer Faces application.
--%>


<script>
function validate(){
  if(document.form1["form1:name"].value ==''){
   alert("Please enter the name");
   return false;
  }
  if(document.form1["form1:password"].value ==''){
    alert("Please enter the Password");
    return false;
  }
  if(document.form1["form1:password1"].value ==''){
   alert("Please enter the Retype Password");
   return false;
  }
  if(document.form1["form1:password1"].value !=document.form1["form1:password"].value){
    alert("Passwords do not match");
    return false;
  }
  if(document.form1["form1:description"].value ==''){
    alert("Please enter the description");
    return false;
  }
return true;
}
</script>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <h1 align="center"> Registration Page</h1>
</head>
<body>

<f:view>

<h:form id="form1" onsubmit="return validate();">
<table align="center">
<tr>
   <td>Name:</td>
   <td><h:inputText id="name" label="Name" value="#{RegistrationBean.name}"/></td>
</tr>
<tr>
   <td>Password:</td>
   <td><h:inputSecret id="password" label="Password" value="#{RegistrationBean.password}"/></td>
</tr>
<tr>
   <td>Retype Password:</td>
   <td><h:inputSecret id="password1" label="Retype Password" value="#{RegistrationBean.password1}"/></td>
</tr>
<tr>
   <td>Description</td>
   <td><h:inputTextarea id="description" rows="10" cols="30" value="#{RegistrationBean.description}"/></td>
</tr>
<tr>
   <td colspan="2" align="center"><h:commandButton id="submit" action="success" value="Submit" /></td>
</tr>
</table>


</h:form>
</f:view>
</body>
</html>




Note:- We can also use the server side validation by writing some methods in the backing beans. This we will learn in coming sections.


  << Prev: Using Managed Bean With JSF -2 Next: Adding Navigation in JSF Application >>
Jsp Jsf Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising