Get jspx at SourceForge.net. Fast, secure and Free Open Source software downloads home | download | demo project | contact us
jspx

Rate this article:

email
comment
Average Rate= 0.0

Simple Form (Custom Validators)

This is the same as the Simple Form (Validators) however this time form validation is custom server side validation. By this way it is very easy to use server side java code to validate the user data entry.

Html file
  1. The standard validators are replaced with custom validator, The following rules are applied.
    1. The user name cannot be empty and cannot be "jspx"
    2. Password should be 8 characters
    3. Mobile should be numeric
    4. Free email services (gmail, yahoo, hotmail)
    5. You should be 13 years.

Demo

Username:
Password:
Mobile:
email:
Birthdate:
Martial Status:

Source Code:

<page contoller="org.bay.jspx.demo.live.SimpleForm" >
	<html>
		<body>
			<form>
			<ajaxPanel>
					<table cellspacing="20px;"  class="demotext">
						<tr>
							<td>
							<table>
								<tr>
									<td> Username: </td>
									<td>
									<input type="text" id="username" />
									<validator type="custom" onServerValidate="validateUsername"  control_to_validate="username"
									 group="x" message="username cannot be empty" />
									</td>
								</tr>
								<tr>
									<td> Password: </td>
									<td>
									<input type="password" id="password" />
									<validator type="custom" onServerValidate="validatePassword"  control_to_validate="password"
									 group="x" message="password is required" />
									</td>
								</tr>
								<tr>
									<td> Mobile: </td>
									<td>
									<input type="text" id="mobile" />
									<validator type="custom" onServerValidate="validateMobile" type="custom" control_to_validate="mobile"
									 group="x" message="numbers only"  indicator="numbers"/>
									</td>
								</tr>
								<tr>
									<td> email: </td>
									<td>
									<input type="text" id="email" />
									<validator type="custom" onServerValidate="validateEmail" type="custom" control_to_validate="email" 
									group="x" message="free email services are not allowed" />
									</td>
								</tr>
								<tr>
									<td> Birthdate: </td>
									<td>
									<calendar id="birthdate" title="birth date"/>
									<validator type="custom" onServerValidate="validateBirthday" type="custom" control_to_validate="birthdate" 
									group="x" message="you should be at least 13" />
									</td>
								</tr>
								<tr>
									<td> Martial Status: </td>
									<td>
									<select id="mstatus">
										<option value=""> -----</option>
										<option value="Single"> Single </option>
										<option value="Married"> Married </option>
										<option value="Divorced"> Divorced </option>
										<option value="In relationship"> In relationship </option>
									</select>
									</td>
								</tr>
								<tr>
									<td colspan="2" align="right">
									<input type="button" onserverclick="printUser" value="save" 
									submitonce="true" group="x"/>
									</td>
								</tr>
							</table>
							</td>
							<td valign="top">
							<table id="userData" rendered="false">
								<tr>
									<td> Username: </td>
									<td> ${this.usernameVal} </td>
								</tr>
								<tr>
									<td> Password: </td>
									<td> ${this.passwordVal} </td>
								</tr>
								<tr>
									<td> Mobile: </td>
									<td> ${this.MobileVal} </td>
								</tr>
								<tr>
									<td> email: </td>
									<td> ${this.emailVal} </td>
								</tr>
								<tr>
									<td> Birthdate: </td>
									<td> ${this.birthdateVal} </td>
								</tr>
								<tr>
									<td> Martial Status: </td>
									<td> ${this.mstatusVal} </td>
								</tr>
							</table></td>
						</tr>
					</table>
				</ajaxPanel>
			</form>
		</body>
	</html>
</page>
				
package org.bay.jspx.demo.live;

import eg.java.net.web.jspx.engine.annotation.JspxWebControl;
import eg.java.net.web.jspx.ui.controls.WebControl;
import eg.java.net.web.jspx.ui.controls.html.GenericWebControl;
import eg.java.net.web.jspx.ui.controls.html.elements.Calendar;
import eg.java.net.web.jspx.ui.controls.html.elements.inputs.Password;
import eg.java.net.web.jspx.ui.controls.html.elements.inputs.TextBox;
import eg.java.net.web.jspx.ui.controls.html.elements.select.Select;
import eg.java.net.web.jspx.ui.pages.Page;

public class SimpleForm extends Page
{

	@JspxWebControl
	TextBox username;
	@JspxWebControl
	Password password;
	@JspxWebControl
	TextBox email;
	@JspxWebControl
	Calendar birthdate;
	@JspxWebControl
	Select mstatus;
	@JspxWebControl
	TextBox mobile;

	@JspxWebControl
	GenericWebControl userData;

	public void printUser(WebControl sender, String args)
	{
		usernameVal = username.getValue();
		passwordVal = password.getValue();
		emailVal = email.getValue();
		if (birthdate.getDate() != null)
			birthdateVal = birthdate.getDate().toString();
		mstatusVal = mstatus.getValue();
		mobileVal = mobile.getValue();
		userData.setRendered(true);
	}
	
	public void validateUsername(WebControl invoker, String args)
	{
		// [Apr 23, 2012 1:45:50 PM] [Amr.ElAdawy] [if the username is empty or "jspx" then reject it]
		TextBox userNameCont = (TextBox) invoker;
		if (userNameCont.getValue().length() == 0 || userNameCont.getValue().equals("jspx"))
			throw new RuntimeException("");
	}

	public void validatePassword(WebControl invoker, String args)
	{
		// [May 6, 2012 6:12:40 PM] [amr.eladawy] [any logic can be set here to valiadte password strength]
		Password pwCont = (Password) invoker;
		if (pwCont.getValue().length() < 8)
			throw new RuntimeException();
	}

	public void validateMobile(WebControl invoker, String args)
	{
		// [May 6, 2012 6:23:49 PM] [amr.eladawy] [validate that there are only numbers entered ]
		TextBox mCont = (TextBox) invoker;
		if (mCont.getValue().length() > 0)
			Long.parseLong(mCont.getValue());
	}

	public void validateEmail(WebControl invoker, String args)
	{
		TextBox mailCont = (TextBox) invoker;
		if (mailCont.getValue().length() > 0)
		{
			String mail = mailCont.getValue().toLowerCase();
			if (mail.contains("gmail") || mail.contains("yahoo") || mail.contains("hotmail"))
				throw new RuntimeException();
		}

	}

	public void validateBirthday(WebControl invoker, String args)
	{
		Calendar bd = (Calendar) invoker;
		if (bd.getValue().length() > 0)
		{
			Date dbDate = bd.getDate();
			long age = System.currentTimeMillis() - dbDate.getTime();
			if (age / 1000 / 60 / 60 / 24 / 365.25 < 13)
				throw new RuntimeException();
		}
	}
	String usernameVal;
	String passwordVal;
	String emailVal;
	String birthdateVal;
	String mstatusVal;
	String mobileVal;

	public String getUsernameVal()
	{
		return usernameVal;
	}

	public String getPasswordVal()
	{
		return passwordVal;
	}

	public String getEmailVal()
	{
		return emailVal;
	}

	public String getBirthdateVal()
	{
		return birthdateVal;
	}

	public String getMstatusVal()
	{
		return mstatusVal;
	}
	public String getMobileVal()
	{
		return mobileVal;
	}
}