asp.net MaxLengthValidator 最大長度驗證控制項代碼

來源:互聯網
上載者:User
/// <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); } }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.