//函数名:fucCheckNUM
//功能介绍:检查是否为数字
//参数说明:要检查的数字
//返回值:1为是数字,0为不是数字
function fucCheckNUM(NUM)
{
var i,j,strTemp;
strTemp="0123456789";
if ( NUM.length== 0)
return 0
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//说明有字符不是数字
return 0;
}
}
//说明是数字
return 1;
}
//函数名:chkemail
//功能介绍:检查是否为Email Address
//参数说明:要检查的字符串
//返回值:0:不是  1:是
function chkemail(a)
{ var i=a.length;
var temp = a.indexOf('@');
var tempd = a.indexOf('.');
if (temp > 1) {
if ((i-temp) > 3){

if ((i-tempd)>0){
return 1;
}

}
}
return 0;
}
//函数名:chkdate
//功能介绍:检查是否为日期
//参数说明:要检查的字符串
//返回值:0:不是日期  1:是日期
function chkdate(datestr)
{
var lthdatestr
if (datestr != "")
lthdatestr= datestr.length ;
else
lthdatestr=0;

var tmpy="";
var tmpm="";
var tmpd="";
//var datestr;
var status;
status=0;
if ( lthdatestr== 0)
return 0
for (i=0;i<lthdatestr;i++)
{ if (datestr.charAt(i)== '-')
{
status++;
}
if (status>2)
{
//alert("Invalid format of date!");
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)
//tempdate= new String (year+month+day);
//alert(tempdate);
if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
{
//alert("Invalid format of date!");
return 0;
}
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
{
//alert ("Invalid month or day!");
return 0;
}
if (!((year % 4)==0) && (month==2) && (day==29))
{
//alert ("This is not a leap year!");
return 0;
}
if ((month<=7) && ((month % 2)==0) && (day>=31))
{
//alert ("This month is a small month!");
return 0;

}
if ((month>=8) && ((month % 2)==1) && (day>=31))
{
//alert ("This month is a small month!");
return 0;
}
if ((month==2) && (day==30))
{
//alert("The Febryary never has this day!");
return 0;
}

return 1;
}

解决方案 »

  1.   

    我想用jsp,servlet来做,我担心人家不通过我的页面提交数据,那么我的客户端就没有用啦。
      

  2.   

    判断EMAIL
    public boolean checkmail(String smail) {
        boolean b = false;
        int point = smail.indexOf("@");
        String temp1 = smail.substring(0,point);
        String temp2 = smail.substring(point+1);
        if(temp1.indexOf(".")==-1) {
            b = true; 
        }
        if(temp2.indexOf(".")==-1) {
            b = false; 
        }else {
            if(temp2.length()-temp2.lastIndexOf()>3) {
                b = false; 
            }
        }
        return b;
    }判断是否为数字组成的字串  public static boolean isNumber(String validString){
          byte[] tempbyte=validString.getBytes();
          for(int i=0;i<validString.length();i++) {
              //by=tempbyte[i];
              if((tempbyte[i]<48)||(tempbyte[i]>57)){
                  return false;
              }
          }
          return true;
      }判断字符串是否为只包括字母和数字  public static boolean isChar(String validString){
          byte[] tempbyte=validString.getBytes();
          for(int i=0;i<validString.length();i++) {
              //  by=tempbyte[i];
              if((tempbyte[i]<48)||((tempbyte[i]>57)&(tempbyte[i]<65))||(tempbyte[i]>122)||((tempbyte[i]>90)&(tempbyte[i]<97))) {
                  return false;
              }
          }
          return true;
      }
    判断字符串是否只包括字母  public static boolean isLetter(String validString){
        byte[] tempbyte=validString.getBytes();
        for(int i=0;i<validString.length();i++) {
            //by=tempbyte[i];
            if((tempbyte[i]<65)||(tempbyte[i]>122)||((tempbyte[i]>90)&(tempbyte[i]<97))) {
                return false;
            }
        }
        return true;
      }判断日期有效性,记得import java.text.*;
        public static boolean validateDate(String vDate) {
            SimpleDateFormat dateFormat = new SimpleDateFormat();
            try {
                dateFormat.applyPattern("yyyy-MM-dd");
                dateFormat.setLenient(false);
                Date date = dateFormat.parse(vDate);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
      

  3.   

    最好还是放在客户端吧
    用javascript来做
      

  4.   

    EMAIL
    if((form.e_mail.value=="")||(form.e_mail.value.indexOf('@',0)==-1)||
      (form.e_mail.value.indexOf('.',0)==-1)||(form.e_mail.value.length<6))
      {
         alert("请你输入合法的E-mail地址!");
         form.e_mail.focus();
     return false;
      }