欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net MaxLengthValidator 最大长度验证控件代码

程序员文章站 2024-03-08 13:32:40
复制代码 代码如下:/// /// textbox的最大长度验证器,如果是varchar,char字段类型,需要此验证 ///
复制代码 代码如下:

/// <summary>
/// textbox的最大长度验证器,如果是varchar,char字段类型,需要此验证
/// </summary>
[description("对maxlength进行验证")]
[toolboxdata("<{0}:maxlengthvalidator runat=server></{0}:maxlengthvalidator>")]
public class maxlengthvalidator : basevalidator
{
/// <summary>
/// 获取或设置是否为非nvarchar,nchar字段验证
/// </summary>
[defaultvalue(true)]
public bool validatefornonnationalfield
{
get
{
var data = this.viewstate["validatefornonnationalfield"];
if (data == null)
{
return true;
}
else
{
return (bool)data;
}
}
set
{
this.viewstate["validatefornonnationalfield"] = value;
}
}

/// <summary>
/// 构造函数
/// </summary>
public maxlengthvalidator()
{
this.validatefornonnationalfield = true;
}

protected override bool evaluateisvalid()
{
var targetcontrolname = this.controltovalidate;
if (targetcontrolname == null)
{
//没有目标验证控件,验证通过
return true;
}

var targetcontrol = this.namingcontainer.findcontrol(targetcontrolname) as textbox;
if (targetcontrol == null)
{
//目标控件不是文本框或不存在,验证通过
return true;
}

var maxlength = targetcontrol.maxlength;
var targetvalue = this.getcontrolvalidationvalue(targetcontrolname);
var targetlength = 0;
if (string.isnullorempty(targetvalue))
{
targetlength = 0;
}
else
{
if (this.validatefornonnationalfield)
{
targetlength = this.getlengthfornonnationnal(targetvalue);
}
else
{
targetlength = targetvalue.length;
}
}

return targetlength <= maxlength;
}

/// <summary>
/// 获得保存为varchar时,text的长度
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public virtual int getlengthfornonnationnal(string text)
{
if (string.isnullorempty(text))
{
return 0;
}

var length = 0;
for (var i = 0; i < text.tochararray().length; i++)
{
var charcode = (int)text[i];

length++;
if (charcode > 255)
{
length++;
}
}
return length;
}

protected override void addattributestorender(htmltextwriter writer)
{
base.addattributestorender(writer);

if (this.renderuplevel)
{
writer.addattribute("targetcontrolid",
this.getcontrolrenderid(this.controltovalidate));
writer.addattribute("validatefornonnationalfield",
this.validatefornonnationalfield ? "true" : "false");
writer.addattribute("evaluationfunction",
"maxlengthvalidator_validate");

var targetcontrol = this.namingcontainer.findcontrol(this.controltovalidate) as textbox;
if (targetcontrol != null)
{
var maxlength = targetcontrol.maxlength;
writer.addattribute("maxlength",
maxlength.tostring());
}
}
}

protected override void onprerender(eventargs e)
{
var script = @"
function maxlengthvalidator_getnonnationallength(text) {
if (!text){
return 0;
}
var length = 0;
for (var i = 0; i < text.length; i++) {
var charcode = text.charcodeat(i);
length++;
if (charcode > 255) {
length++;
}
}
return length;
}
function maxlengthvalidator_validate(val) {
if (!val) {
return true;
}
var targetid = val.targetcontrolid;
if (!targetid) {
return true;
}
var nonnational = val.validatefornonnationalfield;
if (!nonnational) {
return true;
}
var target = document.getelementbyid(targetid);
if (!target) {
return true;
}
var maxlength = val.maxlength;
if (!maxlength || isnan(maxlength)) {
return true;
}
var targetvalue = target.value;
if (!targetvalue) {
return true;
}
var reallength = targetvalue.length;
if (nonnational == 'true')
{
reallength = maxlengthvalidator_getnonnationallength(targetvalue);
}
return reallength <= parseint(maxlength);
}";
scriptmanager.registerclientscriptblock(this,
typeof(maxlengthvalidator),
"maxlengthvalidator_validate",
script,
true);

base.onprerender(e);
}
}