分享
 
 
 

jstl页面校验的用法

王朝html/css/js·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

How to use validation JavaScript Framework

[Summary:]

1. JavaScript Validation Framework includes three files:

validate.js : used to store the main validation rules

validateConfig.js : used to store the properties of validation framework

commonValidation.js : used to store self defined validation rules;

2. Framework provides the validation to single field except date format checking. The covered validation rules are:

1) Not blank;

2) Length within specified bounds;

3) Minimum length;

4) Maximum length;

5) Number;

6) Number with minimum length;

7) Integer with specified numerical value bounds;

8) Decimal with fixed number of digits;

9) Decimal with a ranged number of digits;

10) Email format;

11) Selected item of index past 0;

12) Minimum and maximum of multiple selections;

13) Radio button groups;

14) Date Format;

15) Money Format;

16) Phone Format;

17) Custom pattern;

1. Check to see if the field is blank

Syntax

alt="blank"

Errors if

Field is left blank -OR- only white space characters are present in it's value

Notes

White space characters include Space, Tab, Carriage Return, New line, Formfeed

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

2. Check for a minimum length within specified bounds

Syntax

alt="length|min|max"

minInteger. Sets minimum value accepted.

maxInteger. Sets maximum value accepted.

Errors if

Field contains characters number is outiside of specified bounds -OR- field is left blank*

Notes

he number following the 'min' in the alt will be the minimum required length, he number following the 'max' in the alt will be the maximum required length. Do not add spaces or underscores.

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

3. Check for a minimum length

Syntax

alt="lengthl|min"

minInteger. Sets minimum length

Errors if

Field does not have at lest min characters in it's value -OR- field is left blank*

Notes

he number following the 'lengthl' in the alt will be the minimum required length. Do not add spaces or underscores.

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

4. Check for a maximum length

Syntax

alt="lengthr|max"

maxInteger. Sets maximum length

Errors if

Field is out the liimited ofmax characters in it's value -OR- field is left blank*

Notes

he number following the 'lengthr' in the alt will be the maximum required length. Do not add spaces or underscores.

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

5. Check for only numbers

Syntax

alt="number"

Errors if

Field contains any non-digit characters -OR- field is left blank*

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

6. Check for only numbers with a minimum length

Syntax

alt="numberl|min"

minInteger. Sets minimum length

Errors if

Field contains any non-digit characters -OR- field is left blank*

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

7. Check for only integers within specified bounds

Syntax

alt="numberr|min|max"

minInteger. Sets minimum value accepted. Enter 'none' for no minimum.

maxInteger. Sets maximum value accepted. Enter 'none' for no maximum

Errors if

Field contains any non-integer characters -OR- integer is outiside of specified bounds -OR- field is left blank*

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

8. Check for valid decimal with fixed number of digits

Syntax

alt="decimal|leftDigits|rightdigits"

leftDigitsInteger. Sets exact number of digits that must appear to the left of the decimal

rightDigitsInteger. Sets exact number of digits that must appear to the right of the decimal

Errors if

Field data does not match decimal format specified -OR- is left blank*

Notes

Wildcards (*) can be used for either parameter, specifying any number of digits for that side

Examples

decimal|*|2 will match '1234.12' -OR- '.34' -OR- '1234567890.00'

decimal|3|* will match '100.1' -OR- 123.987 -OR- '987.12345678'

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

9. Check for valid decimal with a ranged number of digits

Syntax

alt="decimalr|leftDigitsMin|leftDigitsMax|rightDigitsMin|rightDigitsMax"

leftDigitsMin Integer. Sets minimum number of digits that must appear to the left of the decimal

leftDigitsMax Integer. Sets maximum number of digits that must appear to the left of the decimal

rightDigitsMin Integer. Sets minimum number of digits that must appear to the right of the decimal

rightDigitsMax Integer. Sets maximum number of digits that must appear to the right of the decimal

Errors if

Field data does not match decimal format specified -OR- is left blank*

Notes

Wildcards (*) can be used for any parameter, specifying any number of digits for that value

Examples

decimalr3|*|2|3 ( '123.12' -OR- '12345678.12' -OR- '9876.123' )

decimalr0|2|1|* ( '.1' -OR- '23.123456789' -OR- '1.98' )

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

10. Check for valid email format

Syntax

alt="email"

Errors if

Field data does not match a valid email format -OR- is left blank*

Formats

user@domain.suffix ( john@internet.com )

user.sub@domain.suffix ( john.doe@internet.com )

user.sub@domain.subd.suffix ( john.doe@email.internet.com )

user@ip ( john@[192.168.1.1] )

And several combinations of the above formats

Object compatibility

INPUT type=text, INPUT type=password, TEXTAREA

11. Check for selected index past 0

Syntax

alt="select"

Errors if

Initial index (0) of the SELECT object is selected (indicating no change made)

Object compatibility

SELECT multiple=false

12. Check for min/max of multiple selections

Syntax

alt="selectm|minS|maxS"

minSInteger. Sets minimum allowed selections. 0 for no minimum.

maxSInteger. Sets maximum allowed selections. Enter 999 or * for no maximum.

Errors if

Number of selected options does not fall within specified min/max values

Object compatibility

SELECT multiple=true

13. Radio-button groups

Syntax

alt="radio"

Errors if

None of the buttons in the radio-button group are checked

Notes

Only put the validator attribute on the first button in the group

Object compatiblity

INPUT type=radio

14. Date format

Syntax

dateFormat = "day" in first textbox;

dateFormat = "month" in second textbox;

dateFormat = "year" in third textbox;

dateFormat = "hour" in four textbox and

dateFormat = "minute" in fifth textbox

if these two textbox exist.

Errors if

Field data does not match valid date format. For example,

31/02/2004 is invalid.

Notes

If you want to make these date fields are mandatory, please

add the alt = “blank” in every textbox.

Set “displayName” property which is used to display when field

data is invalid.

15. Money Format

Syntax

alt="money"

Errors if

Field data does not match the money format

Notes

Object compatiblity

INPUT type=text,

16. Phone Format

Syntax

alt="phone"

Errors if

Field data does not match the phone format

Notes

Object compatiblity

INPUT type=text,

17. Specify Custom Pattern

Syntax

alt="custom" pattern="pattern"

patternAny valid PCRE pattern

Errors if

Field data does not validate against set pattern -OR- is left blank

Notes

Unlike most of the other fValidate validators, this requires the separare, custom HTML attribute 'pattern' for the specification of the pattern. Use of custom error message recommended.

Object compatiblity

INPUT type=text, INPUT type=password, TEXTAREA

3. If you want to validate the relationship between two fields, you should provide the methods by yourself. The validate.js will provide the method to define the error message and some methods to help you do that, such as constructDate, commonValidateNumberR. If self defined validation is used in several JSPs, you can put these validations into commonValidation.js file. Here is the method lists:

Methods used to process error:

1) errorProcess(errorField, errorMessage)

Description: Write the errorMessage in the specified division by yourself and tell user which field data is invalid;

Parameters: errorField — the name of error elements, you can pass one element name, or a array of element name;

errorMessage — the displayed message;

Return: void;

Example: Show as below;

2) checkError(form)

Description: Check the array which contains the error fields, if the array is empty,

return true, else return false;

Parameters: form — the name of form;

Return: true or false;

Example:Show as below;

Methods used to help you apply self defined validation rules:

1) commonValidateNumberR(field,lb,ub)

Description: valid integers within specified bounds;

Parameters: field — the data you wanna validate

lb — the minimum number

ub — the maximum number;

Return: true or false;

Example: commonValidateNumberR(hour,0,23), if hour < 0 or hour > 23, return false, else return ture;

2) constructDate(dayObj,monthObj,yearObj)

Description: construct the date obj according the input field data;

Parameters: dayObj — day field;

monthObj — month field;

yearObj — year field;

Return: Date Object;

Here is a sample:

<script language="Javascript">

function ttsValidateForm(Frm)

{

if(validateForm(Frm)&& checkBailAmount(Frm)&& checkRelativeDate(Frm))

return true;

else

{

// Because no matter validateForm method returns true or false, the self defined methods are still invoked.

checkBailAmount(Frm);

checkRelativeDate(Frm);

return false;

}

}

function checkBailAmount(Frm)

{

if(Frm.conductCode.value == '*' && !commonValidateBlank(Frm.bailAmount.value))

{

errorProcess("bailAmount","The bailAmout is missing!");

return checkError(Frm);

}

return true;

}

function checkRelativeDate(Frm)

{

if(commonValidateBlank(Frm.locatedDay.value) &&

commonValidateBlank(Frm.locatedMonth.value) &&

commonValidateBlank(Frm.locatedYear.value))

{

var locDayObj = Frm.locatedDay;

var locMonthObj = Frm.locatedMonth;

var locYearObj = Frm.locatedYear;

var asAtDayObj = Frm.asAtDateDay;

var asAtMonthObj = Frm.asAtDateMonth;

var asAtYearObj = Frm.asAtDateYear;

if(constructDate(locDayObj,locMonthObj,locYearObj) > constructDate(asAtDayObj,asAtMonthObj,asAtYearObj))

{

var errorField = new Array();

errorField.push(locDayObj.name);

errorField.push(locMonthObj.name);

errorField.push(locYearObj.name);

errorProcess(errorField,"As At Date must later than Located Date!");

return checkError(Frm);

}

return true;

}

</script>

<form name=frmQuery method="post" onsubmit="return ttsValidateForm(this)">

<tr class="content_body">

<td width="150" class="content_field">As At Date/Time*</td>

<td class="content_field_v">

<input name="asAtDateDay" class="inputMonth" dateFormat="day" size=2 maxlength="2" alt="blank" displayName="As At Date/Time"> /

<input name="asAtDateMonth" class="inputDate" dateFormat="month" size=2 maxlength="2" alt="blank" > /

<input name="asAtDateYear" class="inputYear" dateFormat="year" size=4 maxlength="4" alt="blank" >

<input name="asAtTimeHour" class="inputDate" dateFormat="hour" size=2 maxlength="2" alt="blank"> :

<input name="asAtTimeMinute" dateFormat="minute" class="inputDate" size=2 maxlength="2" alt="blank">

</td>

</tr>

<tr class="content_body">

<td width="150" class="content_field" >Located Date</td>

<td class="content_field_v" >

<input name="locatedDay" class="inputMonth" dateFormat="day" size=2 maxlength="2" displayName="Located Date">

/

<input name="locatedMonth" class="inputDate" dateFormat="month" size=2 maxlength="2" >

/

<input name="locatedYear" class="inputYear" dateFormat="year" size=4 maxlength="4" >

<input name="locatedHour" class="inputDate" dateFormat="hour" maxlength="2">

:

<input name="locatedMinute" class="inputDate" dateFormat="minute" maxlength="2">

</td>

</tr>

</form>

[Some Tips:]

1. The validation about date format: the thing you will do just is setting the "dateFormat" property of the three or five date fields as “day”, “month”, “year”, “hour” and “minute”. Just like this: dateFormat="day", dateFormat="month". And the alt isn't required any more. If you expect the date fields are mandatory, you should use alt="blank" for each fields.

2. You should define the error message by yourself except the checking blank. Add new property “msg” in you Html Tag, just like this:

<input name="registrationMark" alt="length|1|6" msg="Registration Mark is invalid!">

3. When you apply self defined validation, please pay attention to applying the constraint. Just like this showing in the sample:

If (commonValidateBlank(Frm.locatedDay.value) &&

commonValidateBlank(Frm.locatedMonth.value) &&

commonValidateBlank(Frm.locatedYear.value))

You must know that there is no priority difference in validation process. The validation which framework provides and self defined validation have the same priority.

4. When you apply self defined validation, you must apply one specified validation method, just like ttsValidateForm method in the sample. And in the form Html Tag, you set onsubmit = “ttsValidateForm(this)”.If you have any self defined method applying, set onsubmit = “validateForm(this)”.

5. If the field is optional, you can set the “bok” into the “alt” property, just like this: alt = “number|bok”. If you set this pattern, validation will be applied only when user input data in this field.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有