时间:2021-05-26
复制代码 代码如下:
/// <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);
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
我们开发ASP.NET站点时,如果将jQueryUIDatepicker与ASP.NET的验证控件(如:RequiredFieldValidator)组合使用:
此控件是asp.net中的验证控件RequiredFieldValidator,RangeValidator,RegularExpressionValidato
ASP.NET网站页和控件框架的建设开发ASP.NET页和控件框架是一种编程框架,它在Web服务器上运行,可以动态地生成和呈现ASP.NET网页。可以从任何浏览
前段时间看到园里有位高手写了个纯css+js的一个效果,我把它和asp.net验证控件结合起来,用这位高手写的效果做为asp.net难控件的提示效果。如下图和以
ASP.NET提供了丰富的数据验证控件,但这个验证功能都必须运用在服务器控件中;在复合验证数据时使用也不是很方便(虽然拖放控件很方便,但拖放数增多和设置相关属性