自定义验证控件CustomValidator
客户端
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
服务器端
private void CustomValidator1_ServerValidate_1(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string lname=args.Value;
if(DB.judge(lname))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
定义的类中的静态方法
public static bool judge(string lname)
{
SqlConnection con =DB.creatconnection();
con.Open();
SqlCommand cmd =new SqlCommand("select count(*) from login where lname='"+lname+"'",con );
int count =Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
return true;
}
else {
return false;
}
其他验证控件比较简单,在这里就不在多说了,有一点需要提示一下,就是在后台代码中如果验证结束,我们可以通过
if(page.isValid){}
判断即可!
电话号码手机验证:
SetFocusOnError="True" ValidationExpression="((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)">
邮箱验证:
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
转载于:https://www.cnblogs.com/shuang121/archive/2011/02/22/1961633.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
