Client side validation taglib ver. 2.2


    Custom JSP taglib. Provides a client side validation service. Taglib generates an appropriate JavaScript code for checking mandatory fields in your forms.

You have to use this taglib with Form tags. Body tag required lets you define mandatory fields. Let we see an example. Suppose we have a simple form with one field and this field is mandatory:
 


<%@ taglib uri="formtag.tld" prefix="html" %>
<%@ taglib uri="taglib.tld" prefix="f" %>

<html:form method="post" action="http://host/cgi-bin/script">
   <input type=text name=username>
   <f:required name="username">Please, type your name</f:required>
</html:form>

Here tag required describes a field username as a mandatory field. Tag's body defines an error message in case of this field is empty.

Also this tag lets you describe some constraint restrictions for fields (length, minimal and maximal value etc.). E.g.:
 


<f:required name="A" integer="true" minValue="1">field A must a positive integer</f:required>
<f:required name="B" minValue="1" maxValue="10">field B is a numeric in [1 - 10] range</f:required>

<!-- C is a group of radio buttons and one of them must me checked -->
<f:required name="C" checked="true"/>

<f:required name="D" dateFormat="yyyy-mm-dd">field D must be a date</f:required>

Attribute selectionNotEquals is useful if you have for example some drop down box (HTML select control) with a prompt phrase (e.g. Please, select ...). You can use a value for such option as a parameter selectionNotEquals.

Also you can provide for this tag a name of your own JavaScript function for validation. This function accepts current form as a parameter and must return a boolean value.

You can provide a regular expression for validation. For example, this declaration tests email address:
 


<f:required name="email" regExp="^[\\\\w-_\\.]*[\\\\w-_\\.]\\@[\\\\w]\\.+[\\\\w]+[\\\\w]$">Please, type email address</f:required>

and this expression tests US phone number (some like (800)123-4566 or 1(800) 123-4566 etc.):
 


<f:required name="phone" regExp="^[01]?\\\\s*[\\(\\.-]?(\\\\d{3})[\\)\\.-]?\\\\s*(\\\\d{3})[\\.-]?\\\\s*(\\\\d{4})">Please, type phone number</f:required>

Tags are:

required

Body tag. Describes a mandatory field. Tag's body defines an error message. By default (if body is empty) tag will use message This field is mandatory. Parameters are:

1) name field's name.
2) minLength Optional parameter. Describes a minimal length for string parameter. Default value is 1.
3) maxLength Optional parameter. Describes a maximal length for string parameter.
4) minValue Optional parameter. Describes a minimal value for numeric parameter.
4) maxValue Optional parameter. Describes a maximal value for numeric parameter.
5) integer Optional parameter. Possible values are true or false. If this value is true than field value must be a valid integer. Default value is false
6) numeric Optional parameter. Possible values are true or false. If this value is true than field value must be a valid numeric. Default value is false
7) creditCard Optional parameter. Possible values are true or false. If this value is true than field value must be a valid credit card number. Default value is false
8) dateFormat Optional parameter. Describes a format for date field. Possible values are yyyy-mm-yy, dd/mm/yyyy etc. where yyyy presents a year, mm presents a month and dd presents a day. If this parameter is defined than field value must be a valid date according to the given pattern.
9) selectionNotEquals Optional parameter. Describes a value to compare for lists.
10) checked Optional parameter. Possible values are true or false. If this value is true tag assumes that this filed is a group of radio buttons or checkboxes and at least one of them must be checked. Default value is false.
11) regExp Optional parameter. Describes a regular expression for tested data.
12) customFunction Optional parameter. Describes a name for your own validation function.
13) alert Optional parameter. Describes a name for your alert function. By default it is built-in JavaScript alert
14) cond Optional parameter. Describes a boolean value tag's behavior depends on. Default value is true

for downloading:

Library: checktag.jar    Description: taglib.tld

In order to use this taglib you have to download Form taglib also.

© Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags suite: