#region 用户验证控件
// 检查用户是否存在
protected void CustomValidatorUserExits_ServerValidate(object source, ServerValidateEventArgs args)
{
string name = args.Value;
db.DBconn();
db.GetSql = "select count(*) from userinfo where username='" + name + "'";
int count = Convert.ToInt32(db.ExecuteScalar());
if (count > 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
// 检查密码强度
protected void CustomValidatorLength_ServerValidate(object source, ServerValidateEventArgs args)
{
// 输入的密码长度少于六位
if (args.Value.Length < 6)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
#endregion
ServerValidate event handler 是erverValidateEventArgs 类的实例. 拥有Value IsValid ValidateEmptyText(空值时是否验证)属性
只用服务器端的验证事件而没有客户端的话,必须提交了才知道错误,不能在提交前显示错误信息。
用可客户端验证的话,可能会在mouseout,change时进行验证并显示错误在页面上。
protected void CustomClientValidate(source, args)
{
if (args.Value.length > 10)
args.IsValid = false;
else
args.IsValid = true;
}
与其它验证控件不同的是,CustomValidator可以在输入为空值时决定是否验证。alidateEmptyText="true",默认为false,即为空时不验证(此时跟其它控件是一样的都不验证)。
与其它验证控件不同的是,CustomValidator可以不绑定到某一表单控件,即ControlToValidate属性不是必须的。
// 检查用户是否存在
protected void CustomValidatorUserExits_ServerValidate(object source, ServerValidateEventArgs args)
{
string name = args.Value;
db.DBconn();
db.GetSql = "select count(*) from userinfo where username='" + name + "'";
int count = Convert.ToInt32(db.ExecuteScalar());
if (count > 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
// 检查密码强度
protected void CustomValidatorLength_ServerValidate(object source, ServerValidateEventArgs args)
{
// 输入的密码长度少于六位
if (args.Value.Length < 6)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
#endregion
ServerValidate event handler 是erverValidateEventArgs 类的实例. 拥有Value IsValid ValidateEmptyText(空值时是否验证)属性
只用服务器端的验证事件而没有客户端的话,必须提交了才知道错误,不能在提交前显示错误信息。
用可客户端验证的话,可能会在mouseout,change时进行验证并显示错误在页面上。
protected void CustomClientValidate(source, args)
{
if (args.Value.length > 10)
args.IsValid = false;
else
args.IsValid = true;
}
与其它验证控件不同的是,CustomValidator可以在输入为空值时决定是否验证。alidateEmptyText="true",默认为false,即为空时不验证(此时跟其它控件是一样的都不验证)。
与其它验证控件不同的是,CustomValidator可以不绑定到某一表单控件,即ControlToValidate属性不是必须的。