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 );
}
}