Isword 发表于 2007-1-27 21:36:47

轻轻的,我走了

zhoufan 发表于 2007-1-27 22:16:27

可惜我不懂C#啊,哎~~

JAY 发表于 2007-1-27 23:13:41

又见拼接字符串~

SWizard 发表于 2007-2-2 01:28:43

个人更倾向于JS与后台两者都进行输入验证,另外.NET自带的验证控件也可以用来偷懒一用吖。

gameboyLV 发表于 2007-1-27 21:23:08

[C#]一个小东西--无敌校验函数

原来一直写JavaScript来校验输入,今天终于花了一点时间写了一个通用的类.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (IsPostBack)
      return;
      FormCheck.Validate(Page, txtUserName, "用户名", FormCheck.CheckType.NotNull);
      FormCheck.Validate(Page, txtPassword, "密码", FormCheck.CheckType.MinLength, 6);
      FormCheck.Validate(Page, txtBirthDay, "出生日期", FormCheck.CheckType.Date);
      FormCheck.Validate(Page, txtAge, "年龄", FormCheck.CheckType.RangeInt, 0, 99);
      FormCheck.Validate(Page, txtPrice, "价格", FormCheck.CheckType.MinNumber, 0);
      FormCheck.Validate(Page, txtNumber, "数量", FormCheck.CheckType.MinInt, 0);
   }
}


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public static class FormCheck
{
   /// <summary>
   /// 需要校验的字段类型
   /// </summary>
   public enum CheckType
   {
      /// <summary>
      /// 字符串
      /// </summary>
      NotNull,
      /// <summary>
      /// 日期
      /// </summary>
      Date,
      /// <summary>
      /// 数字
      /// </summary>
      Number,
      /// <summary>
      /// 整数
      /// </summary>
      Int,
      /// <summary>
      /// 字符串,最大长度
      /// </summary>
      MaxLength,
      /// <summary>
      /// 字符串,长度
      /// </summary>
      Length,
      /// <summary>
      /// 字符串,最小长度
      /// </summary>
      MinLength,
      /// <summary>
      /// 整数,最大值
      /// </summary>
      MaxInt,
      /// <summary>
      /// 整数,范围值
      /// </summary>
      RangeInt,
      /// <summary>
      /// 整数,最大值
      /// </summary>
      MinInt,
      /// <summary>
      /// 数字,最大值
      /// </summary>
      MaxNumber,
      /// <summary>
      /// 数字,范围值
      /// </summary>
      RangeNumber,
      /// <summary>
      /// 数字,最小值
      /// </summary>
      MinNumber
   }

   /// <summary>
   /// 设置提交时校验
   /// </summary>
   /// <param name="page">页面</param>
   /// <param name="txt">控件</param>
   /// <param name="type">类型</param>
   /// <param name="fieldName">字段名</param>
   /// <param name="parms">参数列表</param>
   public static void Validate(Page page, TextBox txt, string fieldName, CheckType type, params int[] parms)
   {
      //注册校验脚本
      if (!page.ClientScript.IsClientScriptIncludeRegistered("FormCheck"))
      page.ClientScript.RegisterClientScriptInclude("FormCheck", "FormCheck.js");
      //添加校验事件
      if (!page.ClientScript.IsOnSubmitStatementRegistered(txt.ClientID))
      {
      string str = string.Empty;
      foreach (int parm in parms)
         str += ",&#39;" + parm.ToString() + "&#39;";
      str = "if(!check" + type.ToString() + "(getControl(&#39;" + txt.ClientID + "&#39;).value" + str + ",&#39;" + fieldName + "&#39;)){getControl(&#39;" + txt.ClientID + "&#39;).focus();return false;}";
      page.ClientScript.RegisterOnSubmitStatement(page.GetType(), txt.ClientID, str);
      }
   }
}


// JScript 文件
String.prototype.trim= function()
{
   // 用正则表达式将前后空格
   // 用空字符串替代。
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

function getControl(ID)
{
   return document.getElementById(ID);
}

function checkNotNull(inString,fieldName)
{
   if(inString==null)
   {
      alert(fieldName +"必须输入。");
      return false;
   }

   if(inString.trim().length==0)
   {
      alert(fieldName +"必须输入。");
      return false;
   }
   return true;
}

function checkDate(inString,fieldName)
{
   if(!checkNotNull(inString,fieldName)) return false;
   var tempDate;
   var a=new Date(inString);
   var y=a.getFullYear();
   var m=a.getMonth()+1;
   var d=a.getDate();
   var myday=y + "/" + m + "/" + d
   if (myday!=inString)
   {
   alert("请为"+ fieldName +"输入一有效日期(yyyy/m/d)。");
   return false;
   }
   return true;
}

function checkNumber(inString,fieldName)
{
   if(!checkNotNull(inString,fieldName)) return false;
   if(isNaN(inString))
   {
   alert("请为"+ fieldName +"输入一有效数字。");
   return false;
   }
   return true;
}

function checkInt(inString,fieldName)
{
   if(!checkNumber(inString,fieldName)) return false;
   if(inString.indexOf(&#39;.&#39;)>=0)
   {
      alert("请为"+ fieldName +"输入一有效整数。");
      return false;
   }
   return true;   
}

function checkMaxLength(inString,inLength,fieldName)
{
   if(!checkNotNull(inString,fieldName)) return false;
   if(inString.length>inLength)
   {
   alert(fieldName +"的长度必须小于" + inLength +"位。");
   return false;
   }
   return true;
}

function checkLength(inString,inLength,fieldName)
{
   if(!checkNotNull(inString,fieldName)) return false;
   if(inString.length==inLength)
   {
   alert(fieldName +"的长度必须等于" + inLength +"位。");
   return false;
   }
   return true;
}

function checkMinLength(inString,inLength,fieldName)
{
   if(!checkNotNull(inString,fieldName)) return false;
   if(inString.length<inLength)
   {
   alert(fieldName +"的长度必须大于" + inLength +"位。");
   return false;
   }
   return true;
}

function checkRangeInt(inString,inMinValue,inMaxValue,fieldName)
{
   if(!checkInt(inString,fieldName)) return false;
   if(parseInt(inString)<parseInt(inMinValue) ||parseInt(inString)>parseInt(inMaxValue) )
   {
   alert(fieldName +"必须输入一在" + inMinValue+"于"+inMaxValue+"之间的整数。");
   return false;
   }
   return true;
}

function checkMaxInt(inString,inValue,fieldName)
{
   if(!checkInt(inString,fieldName)) return false;
   if(parseInt(inString)>parseInt(inValue))
   {
   alert(fieldName +"必须输入一小于" + inValue +"的整数。");
   return false;
   }
   return true;
}

function checkMinInt(inString,inValue,fieldName)
{
   if(!checkInt(inString,fieldName)) return false;
   if(parseInt(inString)<parseInt(inValue))
   {
   alert(fieldName +"必须输入一大于" + inValue +"的整数。");
   return false;
   }
   return true;
}

function checkMaxNumber(inString,inValue,fieldName)
{
   if(!checkNumber(inString,fieldName)) return false;
   checkNumber(inString,fieldName);
   if(parseFloat(inString)>parseFloat(inValue))
   {
   alert(fieldName +"必须输入一小于" + inValue +"的数字。");
   return false;
   }
   return true;
}


function checkMinNumber(inString,inValue,fieldName)
{
   if(!checkNumber(inString,fieldName)) return false;
   if(parseFloat(inString)<parseFloat(inValue))
   {
   alert(fieldName +"必须输入一大于" + inValue +"的数字。");
   return false;
   }
   return true;
}

function checkRangeNumber(inString,inMinValue,inMaxValue,fieldName)
{
   if(!checkNumber(inString,fieldName)) return false;
   if(parseFloat(inString)>parseFloat(inMaxValue) || parseFloat(inString)<parseFloat(inMinValue))
   {
   alert(fieldName +"必须输入一在" + inMinValue+"于"+inMaxValue+"之间的数字。");
   return false;
   }
   return true;
}
页: [1]
查看完整版本: [C#]一个小东西--无敌校验函数