Sitemap    
  Home Food Photographs IT Parenting Resources Learning  
JAVASCRIPT
jQuery validate form using ajax 1
jQuery validate form using ajax 2
jQuery validate form using ajax 1

Steps
1. Create simple Servlet for form validation. This is for testing and can be replaced with PHP or any other web technology you like
2. Create JSP to display form. Again needed for testing and nothing to do with jQuery.
3. Run
4. Now application is up and running add jquery code
5. Run

1: FormValidationAjax.java
package com.company.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormValidationAjax extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {
		process(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {
		process(request, response);
	}

	public void process(HttpServletRequest request, HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {
		System.out.println("Inside FormValidationAjax::process()");
		String errorMsg = "";
		String param1 = request.getParameter("param1");
		String param2 = request.getParameter("param2");
		String param3 = request.getParameter("param3");

		if(param1.length() < 9)
		{
			errorMsg = errorMsg+"Param1 must be > 8"+"
"; } if(param2.length() < 9) { errorMsg = errorMsg+"Param2 must be > 8"+"
"; } if(param3.length() < 9) { errorMsg = errorMsg+"Param3 must be > 8"+"
"; } request.setAttribute("errorMsg", errorMsg); request.getRequestDispatcher("/jsp/ajaxform.jsp").forward(request, response); } }


2A: ajaxform.jsp
<%
	String ctx = request.getContextPath();
	String errorMsg = (String) request.getAttribute("errorMsg");
	if (errorMsg == null) {
		errorMsg = "";
	}
%>
<html>
<head>
<title>/jsp/ajaxform.jsp</title>
</head>
<body>
<form name="FormValidationAjax" method="post" action="<%=ctx%>/FormValidationAjax">
<div><span style="color:red"><%=errorMsg%></span></div>
<div>Param1 (length must be > 8): <input type="text" value="abc" name="param1" id="param1" /></div>
<div>Param2 (length must be > 8): <input type="text" value="def" name="param2" id="param2" /></div>
<div>Param3 (length must be > 8): <input type="text" value="ghi" name="param3" id="param3" /></div>
<div><input type="submit" name="submit" class="button" id="submit_btn" value="Test" /></div>
</form>
</body>
</html>


2B: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>ServletApp</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<servlet>
		<display-name>FormValidationAjax</display-name>
		<servlet-name>FormValidationAjax</servlet-name>
		<servlet-class>com.company.servlet.FormValidationAjax</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>FormValidationAjax</servlet-name>
		<url-pattern>/FormValidationAjax</url-pattern>
	</servlet-mapping>
</web-app>

Now launch page and add data and submit
URL: http://servername:port/context/jsp/ajaxform.jsp


Servlet validates data


Till this point we have not used jQuery. Now let us add jQuery code
ADD TO DEL.ICIO.US ADD TO DIGG ADD TO FURL ADD TO REDDIT ADD TO STUMBLEUPON ADD TO TECHNORATI FAVORITES ADD TO SQUIDOO ADD TO YAHOO MYWEB ADD TO ASK ADD TO GOOGLE
Post your comments:
Your Name: 
Security check:
Your Comment: 1500 chars
Latest
Wheat Cookies
WheatCookie
Wheat cookies is a healthy snack for your fussy toddler......
Babyfood  2010-Aug-26
Wheat Cookies
WheatCookie
Wheat cookies is a healthy snack for your fussy toddler......
Veg Recipes Baking  2010-Aug-26
Encoding special characters in userinput or on server
Encoding can be done either in Javascript or JAVA encodeURIComponent, escape, java.net.URLEncoder..
Java JSP  2010-Aug-09
Jstl fmt tag i18n formatdate formatcurrency
fmt:setBundle, native2ascii.exe, fmt:message, fmt:setLocale, fmt:formatDate, fmt:formatNumber type=currency..
Java JSP  2010-Aug-05
How to read and write a file
How to read and write a file java.io.BufferedReader,java.io.FileReader, InputStreamReader ...
Java J2SE  2010-Aug-04
jQuery validate form using ajax 2
How to validate/submit form using ajax and jQuery input#, ($.ajax)..
Javascript  2010-Jul-23
jQuery validate form using ajax 1
How to validate/submit form using ajax and jQuery ($.ajax)..
Javascript  2010-Jul-23
Dosa
Dosa batterDosa
Masala dosa is my favourite South Indian dish and I relish it it whenever we go to a South Indian food joint......
Veg Recipes Parathas  2010-Jul-14
Palak Sukka/Spinach Sukka/Palakachi Sukhi Bhaji
Spinach leaves/images/food/vegrecipes/160X120/PALAK METHI SUKHI BHAJI 2 (2).JPG
If you are looking for a palak recipe apart from aloo palak or palak paneer, which quick yet delicious you are at the right place......
Veg Recipes Dry Veg  2010-Jul-13
How to create datasource in RAD Websphere
Create JDBC provider, datasource and JAAS security setup, JDBC connection URLs
Servers Websphere  2010-Jul-08
Get Started 4
Struts2 framework structure, how various components fit together
Java Struts2  2010-Jul-07
How to populate a form when JSP is called first time
This is achieved throught setting bean in request, using frameworks like Struts2, Spring3..
Java JSP  2010-Jul-07
Spring3 And Hibernate 4
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 3
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 1
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07
Spring3 And Hibernate 2
Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
Java Spring  2010-Jul-07