📄 errorcheck.java
字号:
package permission;
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 =
"<script ID=clientEventHandlersJS language=javascript>" + "\n" +
"<!--" + "\n";
this.neededFunction(); // load the needed functions
this.errorCheckStr +=
"function errorCheck() {" + "\n";
}
/***************************************************************************\
* public: export javascript script
* 输出 JAVASCRIPT 脚本
\***************************************************************************/
public String ErrorCheckScript() {
this.errorCheckStr +=
"}" + "\n" +
"-->" + "\n" +
"</script>" + "\n";
return this.errorCheckStr;
}
/***************************************************************************\
* 检查一个文本是否等于另一个文本
* 通常作为密码与确认密码的判断
\***************************************************************************/
public void equalCheck(String inputName1,String inputName2, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName1+".value)==1) {" + "\n" +
" if(document."+formName+"."+inputName1+".value != document."+formName+"."+inputName2+".value) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName2+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是数字
\***************************************************************************/
public void numericCheck(String inputName, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(fucCheckNUM(document."+formName+"."+inputName+".value) == 0) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是数字及其数字的范围
\***************************************************************************/
public void numericCheckArea(String inputName, String errorMsg,int MinLength, int MaxLength) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(isNaN(document."+formName+"."+inputName+".value)) {"+"\n"+
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "else"+
" { if(parseFloat(document."+formName+"."+inputName+".value)>"+MaxLength+"||"+"\n"+
" parseFloat(document."+formName+"."+inputName+".value)<"+MinLength+") {"+"\n"+
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框是否为空
\***************************************************************************/
public void nullCheck(String inputName, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==0) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值的长度
\***************************************************************************/
public void lengthCheck(String inputName, String errorMsg, int MinLength, int MaxLength) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(fucCheckLength(document."+formName+"."+inputName+".value)<"+MinLength+" || " + "\n" +
" fucCheckLength(document."+formName+"."+inputName+".value)>"+MaxLength+") {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是正确的EMAIL格式
\***************************************************************************/
public void emailCheck(String inputName, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(chkemail(document."+formName+"."+inputName+".value) == 0) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是电话号码
\***************************************************************************/
public void telCheck(String inputName, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(fucCheckTEL(document."+formName+"."+inputName+".value) == 0) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是包含给定字串
* 如果不包含,提示重新输入
\***************************************************************************/
public void stringCheck(String inputName, String errorMsg, String string) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(document."+formName+"."+inputName+".value.indexOf(\""+string+"\") == -1) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是包含给禁止的字串
* 如果包含,提示重新输入
\***************************************************************************/
public void denyStrCheck(String inputName, String errorMsg, String string) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if (document."+formName+"."+inputName+".value.length == 0 || " + "\n" +
" document."+formName+"."+inputName+".value.indexOf(\""+string+"\") != -1) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
/***************************************************************************\
* 检查录入框值是否是YYYY-MM-DD的日期格式
\***************************************************************************/
public void dateCheck(String inputName, String errorMsg) {
this.errorCheckStr +=
" if(fucCheckNull(document."+formName+"."+inputName+".value)==1) {" + "\n" +
" if(chkdate(document."+formName+"."+inputName+".value) == 0) {" + "\n" +
" alert(\""+errorMsg+".\");" + "\n" +
" document."+formName+"."+inputName+".focus();" + "\n" +
" return(false);" + "\n" +
" }" + "\n"+
" }" + "\n\n";
}
public void neededFunction() {
this.errorCheckStr +=
"//函数名:fucCheckNUM" + "\n" +
"//功能介绍:检查是否为数字" + "\n" +
"//参数说明:要检查的数字" + "\n" +
"//返回值:1为是数字,0为不是数字" + "\n" +
"function fucCheckNUM(NUM) {" + "\n" +
" var i,j,strTemp;" + "\n" +
" strTemp=\".0123456789\";" + "\n" +
" if ( NUM.length == 0) return 0;" + "\n" +
" for (i=0;i<NUM.length;i++) {" + "\n" +
" j = strTemp.indexOf(NUM.charAt(i));" + "\n" +
" if (j==-1) {" + "\n" +
" //说明有字符不是数字" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" }" + "\n" +
" //说明是数字" + "\n" +
" return 1;" + "\n" +
"}" + "\n\n" +
"//函数名:fucCheckLength" + "\n" +
"//功能介绍:检查字符串的长度" + "\n" +
"//参数说明:要检查的字符串" + "\n" +
"//返回值:长度值" + "\n" +
"function fucCheckLength(strTemp) {" + "\n" +
" var i,sum;" + "\n" +
" sum=0;" + "\n" +
" for(i=0;i<strTemp.length;i++) {" + "\n" +
" if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))" + "\n" +
" sum=sum+1;" + "\n" +
" else" + "\n" +
" sum=sum+2;" + "\n" +
" }" + "\n" +
" return sum;" + "\n" +
"}" + "\n\n" +
"//函数名:fucCheckNull" + "\n" +
"//功能介绍:检查字符串是否为空" + "\n" +
"//参数说明:要检查的字符串" + "\n" +
"//返回值:1:不是空 0:是空" + "\n" +
"function fucCheckNull(strTemp) {" + "\n" +
" if (strTemp.length>0)" + "\n" +
" return 1;" + "\n" +
" else" + "\n" +
" return 0;" + "\n" +
"}" + "\n\n" +
"//函数名:chkemail" + "\n" +
"//功能介绍:检查是否为Email Address" + "\n" +
"//参数说明:要检查的字符串" + "\n" +
"//返回值:0:不是 1:是" + "\n" +
"function chkemail(a) {" + "\n" +
" var i=a.length;" + "\n" +
" var temp = a.indexOf('@');" + "\n" +
" var tempd = a.indexOf('.');" + "\n" +
" if (temp > 1) {" + "\n" +
" if ((i-temp) > 3) {" + "\n" +
" if (tempd!=-1) {" + "\n" +
" return 1;" + "\n" +
" }" + "\n" +
" }" + "\n" +
" }" + "\n" +
" return 0;" + "\n" +
"}" + "\n\n" +
"//函数名:fucCheckTEL" + "\n" +
"//功能介绍:检查是否为电话号码" + "\n" +
"//参数说明:要检查的字符串" + "\n" +
"//返回值:1为是合法,0为不合法" + "\n" +
"function fucCheckTEL(TEL) {" + "\n" +
" var i,j,strTemp;" + "\n" +
" strTemp=\"0123456789-()#\";" + "\n" +
" if (TEL.length == 0) return 0;" + "\n" +
" for (i=0;i<TEL.length;i++) {" + "\n" +
" j=strTemp.indexOf(TEL.charAt(i));" + "\n" +
" if (j==-1) {" + "\n" +
" //说明有字符不合法" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" }" + "\n" +
" //说明合法" + "\n" +
" return 1;" + "\n" +
"}" + "\n\n" +
"//函数名:chkdate (YYYY-MM-DD)" + "\n" +
"//功能介绍:检查是否为日期" + "\n" +
"//参数说明:要检查的字符串" + "\n" +
"//返回值:0:不是日期 1:是日期" + "\n" +
"function chkdate(datestr) {" + "\n" +
" var lthdatestr" + "\n" +
" if (datestr != \"\")" + "\n" +
" lthdatestr= datestr.length ;" + "\n" +
" else" + "\n" +
" lthdatestr=0;" + "\n" +
" var tmpy=\"\";" + "\n" +
" var tmpm=\"\";" + "\n" +
" var tmpd=\"\";" + "\n" +
" //var datestr;" + "\n" +
" var status;" + "\n" +
" status=0;" + "\n" +
" if ( lthdatestr== 0)" + "\n" +
" return 0;" + "\n" +
" for (i=0;i<lthdatestr;i++) {" + "\n" +
" if (datestr.charAt(i)== '-') {" + "\n" +
" status++;" + "\n" +
" }" + "\n" +
" if (status>2) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if ((status==0) && (datestr.charAt(i)!='-')) {" + "\n" +
" tmpy=tmpy+datestr.charAt(i)" + "\n" +
" }" + "\n" +
" if ((status==1) && (datestr.charAt(i)!='-')) {" + "\n" +
" tmpm=tmpm+datestr.charAt(i)" + "\n" +
" }" + "\n" +
" if ((status==2) && (datestr.charAt(i)!='-')) {" + "\n" +
" tmpd=tmpd+datestr.charAt(i)" + "\n" +
" }" + "\n" +
" }" + "\n" +
" year=new String (tmpy);" + "\n" +
" month=new String (tmpm);" + "\n" +
" day=new String (tmpd)" + "\n" +
" if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2)) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) ) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if (!((year % 4)==0) && (month==2) && (day==29)) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if ((month<=7) && ((month % 2)==0) && (day>=31)) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if ((month>=8) && ((month % 2)==1) && (day>=31)) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" if ((month==2) && (day==30)) {" + "\n" +
" return 0;" + "\n" +
" }" + "\n" +
" return 1;" + "\n" +
"}" + "\n\n";
}
/*public static void main(String[] args) {
ErrorCheck ec = new ErrorCheck("testFrom");
String script = ec.ErrorCheckScript();
System.out.println(script);
} */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -