无为清净楼资源网 Design By www.qnjia.com
复制代码 代码如下:
/// <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);
}
}
/// <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);
}
}
标签:
asp.net,最大长度验证
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- ZEN.1996-珍惜所有【华纳】【WAV+CUE】
- 群星《环球国语元素》香港首版[WAV+CUE][1G]
- 周慧敏《玉女天后》原音母版1:1直刻[WAV+CUE][1G]
- 谭咏麟《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- 炉石传说40轮盘术最新卡组代码在哪找 标准40轮盘术卡组代码分享
- 炉石传说亲王贼怎么玩 2024亲王贼最新卡组代码分享
- 炉石传说30.6.2补丁后有什么卡组 30.6.2最强卡组最新推荐
- 模拟之声慢刻CD《蔡琴名曲回顾遇听》[原抓WAV+CUE]
- BruceLiu-WAVES(MusicbySatie)(2024)2CD[24Bit-96kHz]FLAC
- KonstantinKrimmel-MythosSchubertLoewe(2024)[24Bit-96kHz]FLAC
- 2024雷蛇高校挑战赛 嘤式分解助力收官之战
- 海信发布110吋世俱杯官方定制AI电视 引领智能观赛
- 海信发布27英寸显示器大圣G5 Pro:采用自研超解析芯片、友达原厂模组
- 蔡琴《机遇》1:1母盘直刻日本头版[WAV分轨][1.1G]
- 陈百强《与你几分钟的约会》XRCD+SHMCD限量编号版[低速原抓WAV+CUE][994M]