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