Asp.net MaxLengthValidator maximum length verification control code

Source: Internet
Author: User

Copy codeThe Code is as follows:
/// <Summary>
/// The maximum length validators of TextBox. This verification is required for varchar and char fields.
/// </Summary>
[Description ("verify MaxLength")]
[ToolboxData ("<{0}: MaxLengthValidator runat = server> </{0}: MaxLengthValidator>")]
Public class MaxLengthValidator: BaseValidator
{
/// <Summary>
/// Obtain or set whether it is a non-nvarchar or nchar Field Verification
/// </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>
/// Constructor
/// </Summary>
Public MaxLengthValidator ()
{
This. ValidateForNonNationalField = true;
}

Protected override bool EvaluateIsValid ()
{
Var targetControlName = this. ControlToValidate;
If (targetControlName = null)
{
// There is no target verification control. The verification passes
Return true;
}

Var targetControl = this. NamingContainer. FindControl (targetControlName) as TextBox;
If (targetControl = null)
{
// The target control is not a text box or does not exist. It passes verification.
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>
/// Obtain the text length when saved as Varchar
/// </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. addattrifield ("ValidateForNonNationalField ",
This. ValidateForNonNationalField? "True": "false ");
Writer. addattriation( "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 & gt; 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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.