快捷搜索:

用JSP在客户端生成JavaScript代码来实现表单校验

●○●○●○●○●○●○●○●○●○●○●○●○●○●○

○ 作者:刘湛   日期:2000-01-05   jeru@163.net ●

● http://www.cyberlabs.com/~jeru/          ○

○ 迎接造访爪哇人,获取更多资料           ●

●○●○●○●○●○●○●○●○●○●○●○●○●○●○

本日费了一天光阴便是做这个器械,道理很简单,便是用 JSP 在页面的开始部分天生一段代码,

如 errorcheck.jsp 中所示,但法度榜样太长,照样费了我不少光阴来改写。

主法度榜样是名为 ErrorCheck.java ,有了这个 ErrorCheck 的 Bean,我们就再也不用为了表单校验去

写那一大年夜堆烦人的 JavaScript 代码了。ErrorCheck 类已帮我们天生了险些所有你将会用到的校验措施,

如是否为数字,长度的校验,是否为合法email等,你只需在 jsp 页面里调用响应的函数就可以了。

今朝一共有七个函数:

一 检测是否为数字

//输入输入框名和差错提示信息

numericCheck(String inputName,String errorMsg);

二 检测email是否合法

//输入输入框名和差错提示信息

emailCheck(String inputName,String errorMsg);

三 检测电话号码是否合法

//输入输入框名和差错提示信息

telCheck(String inputName,String errorMsg);

四 检测字串长度是否在规定范围那内

//输入输入框名,差错提示信息,最小长度,最大年夜长度

lengthCheck(String inputName,String errorMsg,int min,int max);

五 检测字串中是否不含禁止的字串

//输入输入框名,差错提示信息,禁止字串

denyStrCheck(String inputName,String errorMsg,String str);

六 检测字串中是否含给定字串

//输入输入框名,差错提示信息,指定字串

stringCheck(String inputName,String errorMsg,String str);

七 检测日期款式是否为 "yyyy-mm-dd"

//输入输入框名和差错提示信息

dateCheck(String inputName,String errorMsg);

只要调用一下这个bean,然后用setFromName()设定你的表单名,再调用以上函数,

着末 out.println(yourID.ErrorCheckScript()),就输出了一段 JavaScript 代码了,当然了,

别忘了这个

ok,just enjoy it,本日太累,不想若干,有任何意见请写信给我或在我主页上留言。

注:我调试 errorcheck.jsp 的时刻因办事器的问题不知为何不能用 usebean,setProperty 的措施,

只好 new 了一下,我想你们是应该可以用useBean和setProperty的,自己改一下吧。

===================================== errorcheck.jsp =====================================

Errocheck Test

input a number:

input a emial:

input a telephone:

input a string (length should between 6-8):

input a string (shoulde not contain "jeru"):

input a string (must contain "jeru"):

input a date (yyyy-mm-dd):

===================================== ErrorCheck.java =====================================

package dbclass;

/**

* ErrorCheck v 1.0

*

* 这个类是用来在客户端天生 JavaScript 代码来校验表单的

* 原是版本是同事 Macro 用 PHP 写的,我感到十分好用,再也

* 不用再为那些表单区写烦人的 javascript 代码拉,谢谢他!

* 此次我用 Java 改写,封装成一个类,并修复了少许的 bug,加

* 多了一条校验的功能,它的扩展性很好,今后可能会继承完善。

*

* Mender :

*Jeru Liu

* Homepage :

*http://www.cyberlabs.com/~jeru/

* Email: jeru@163.net

*

*/

import java.io.*;

public class ErrorCheck {

/* public: the javascript string */

String errorCheckStr;

/* public: the form name you used */

public String formName;

public void setFormName(String formName) {

this.formName = formName;

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: constructor functions

*构造函数

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public ErrorCheck() {

this.errorCheckStr =

"" + "

";

return this.errorCheckStr;

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check the numeric

*反省录入框值是否是数字

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void numericCheck(String inputName, String errorMsg) {

this.errorCheckStr +=

"if(fucCheckNUM(document."+formName+"."+inputName+".value) == 0) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check the length

*反省录入框值的长度

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void lengthCheck(String inputName, String errorMsg, int MinLength, int MaxLength) {

this.errorCheckStr +=

"if(fucCheckLength(document."+formName+"."+inputName+".value)"+MaxLength+") {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check the email

*反省录入框值是否是精确的EMAIL款式

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void emailCheck(String inputName, String errorMsg) {

this.errorCheckStr +=

"if(chkemail(document."+formName+"."+inputName+".value) == 0) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check the telephone number

*反省录入框值是否是电话号码

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void telCheck(String inputName, String errorMsg) {

this.errorCheckStr +=

"if(fucCheckTEL(document."+formName+"."+inputName+".value) == 0) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check if the input value contian the prefered string

*反省录入框值是否是包孕给定字串

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void stringCheck(String inputName, String errorMsg, String string) {

this.errorCheckStr +=

"if(document."+formName+"."+inputName+".value.indexOf(""+string+"") != 0) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check if the input value contain the denyed string

*反省录入框值是否是包孕给禁止的字串

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void denyStrCheck(String inputName, String errorMsg, String string) {

this.errorCheckStr +=

"if (document."+formName+"."+inputName+".value.length == 0 || " + "

" +

"document."+formName+"."+inputName+".value.indexOf(""+string+"") != -1) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

/奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫

*public: check the YYYY-MM-DD format date

*反省录入框值是否是YYYY-MM-DD的日期款式

奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫/

public void dateCheck(String inputName, String errorMsg) {

this.errorCheckStr +=

"if(chkdate(document."+formName+"."+inputName+".value) == 0) {" + "

" +

"alert(""+errorMsg+".");" + "

" +

"document."+formName+"."+inputName+".focus();" + "

" +

"return(false);" + "

" +

"}" + "

";

}

public void neededFunction() {

this.errorCheckStr +=

"//函数名:fucCheckNUM" + "

" +

"//功能先容:反省是否为数字" + "

" +

"//参数阐明:要反省的数字" + "

" +

"//返回值:1为是数字,0为不是数字" + "

" +

"function fucCheckNUM(NUM) {" + "

" +

"var i,j,strTemp;" + "

" +

"strTemp="0123456789";" + "

" +

"if ( NUM.length == 0) return 0;" + "

" +

"for (i=0;i=0) && (strTemp.charCodeAt(i) 1) {" + "

" +

"if ((i-temp) > 3) {" + "

" +

"if (tempd!=-1) {" + "

" +

"return 1;" + "

" +

"}" + "

" +

"}" + "

" +

"}" + "

" +

"return 0;" + "

" +

"}" + "

" +

"//函数名:fucCheckTEL" + "

" +

"//功能先容:反省是否为电话号码" + "

" +

"//参数阐明:要反省的字符串" + "

" +

"//返回值:1为是合法,0为分歧法" + "

" +

"function fucCheckTEL(TEL) {" + "

" +

"var i,j,strTemp;" + "

" +

"strTemp="0123456789-()#";" + "

" +

"if (TEL.length == 0) return 0;" + "

" +

"for (i=0;i2) {" + "

" +

"return 0;" + "

" +

"}" + "

" +

"if ((status==0) && (datestr.charAt(i)!=´-´)) {" + "

" +

"tmpy=tmpy+datestr.charAt(i)" + "

" +

"}" + "

" +

"if ((status==1) && (datestr.charAt(i)!=´-´)) {" + "

" +

"tmpm=tmpm+datestr.charAt(i)" + "

" +

"}" + "

" +

"if ((status==2) && (datestr.charAt(i)!=´-´)) {" + "

" +

"tmpd=tmpd+datestr.charAt(i)" + "

" +

"}" + "

" +

"}" + "

" +

"year=new String (tmpy);" + "

" +

"month=new String (tmpm);" + "

" +

"day=new String (tmpd)" + "

" +

"if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2)) {" + "

" +

"return 0;" + "

" +

"}" + "

" +

"if (!((1=month) && (31>=day) && (1=31)) {" + "

" +

"return 0;" + "

" +

"}" + "

" +

"if ((month>=8) && ((month % 2)==1) && (day>=31)) {" + "

" +

"return 0;" + "

" +

"}" + "

" +

"if ((month==2) && (day==30)) {" + "

" +

"return 0;" + "

" +

"}" + "

" +

"return 1;" + "

" +

"}" + "

";

}

/*public static void main(String[] args) {

ErrorCheck ec = new ErrorCheck("testFrom");

String script = ec.ErrorCheckScript();

System.out.println(script);

} */

}

您可能还会对下面的文章感兴趣: