常用的验证

声明:    
是参照别人写的,自己整理了一下

namespace UtilityValidator
{
    using System;
    using System.Text.RegularExpressions;

    public class Validators
    {
 //验证是否是有效日期
        public static bool isValidDate(string strln)
        {
            if (Regex.IsMatch(strln, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$"))
            {
                return (strln.CompareTo("1753-01-01") >= 0);
            }
            return false;
        }

 //验证Email
        public static bool isValidEmail(string strln)
        {
            return Regex.IsMatch(strln, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

 //验证传真
        public static bool isValidFax(string strln)
        {
            return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }

 //验证是否是有效移动电话号码
        public static bool isValidMobil(string strln)
        {
            return Regex.IsMatch(strln, @"^(\d)+[-]?(\d){6,12}$");
        }

 //验证是否只含有字母
        public static bool isValidOnllyChar(string strln)
        {
            return Regex.IsMatch(strln, "^[A-Za-z]+$");
        }

 //验证是否只含有汉字
        public static bool isValidOnllyChinese(string strln)
        {
            return Regex.IsMatch(strln, @"^[\u4e00-\u9fa5]+$");
        }

 //验证是否只含有数字
        public static bool isValidOnlyNumber(string strln)
        {
            return Regex.IsMatch(strln, "^[0-9]+$");
        }

 //验证是否是有效密码
        public static bool isValidPassWord(string strln)
        {
            return Regex.IsMatch(strln, @"^(\w){6,20}$");
        }

 //验证是否是有效电话号码
        public static bool isValidTel(string strln)
        {
            return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }

 //验证是否是有效邮编号码
        public static bool isValidZip(string strln)
        {
            return Regex.IsMatch(strln, "^[a-z0-9 ]{3,12}$");
        }

    }
}




HTML代码的屏蔽
 function mask_HTMLCode(strInput) {
   var myReg = /<(\w+)>/;
   return strInput.replace(myReg, "<$1>");
 }
E-mail地址验证:
 function test_email(strEmail) {
  var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
  if(myReg.test(strEmail)) return true;
  return false;
 }  

 

var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
  
   if(form1.email.value == "" || form1.problem.value == "" || form1.answer.value == "" || form1.username.value == "" ){
    str+="星号为必填项!\n";
   }
  
   if(form1.email.value!=""){
    if(!myReg.test(form1.email.value)) {
     str+="EMAIL输入有误,请重新输入!\n";
    }
   }
  
   if(form1.password.value != form1.password2.value){
    str+="两次密码输入不一致\n";
   }
  
   if(form1.code.value!=""){
   if(!form1.code.value.match(/^[0-9]{6}$/)){
    str+="邮编输入错误,请重新输入!\n";
   }
   }



posted on 2006-12-20 20:29  感動常在  阅读(670)  评论(1编辑  收藏  举报