Copy Code code as follows:
<summary>
Maximum length validator for textbox, if Varchar,char field type, this validation is required
</summary>
[Description ("Validate MaxLength")]
[ToolBoxData ("<{0}:maxlengthvalidator runat=server></{0}:maxlengthvalidator>")]
public class Maxlengthvalidator:basevalidator
{
<summary>
Gets or sets whether validation is not Nvarchar,nchar field
</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>
Constructors
</summary>
Public Maxlengthvalidator ()
{
This. Validatefornonnationalfield = true;
}
protected override bool EvaluateIsValid ()
{
var targetcontrolname = this. ControlToValidate;
if (targetcontrolname = null)
{
No target validation control, validation through
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 and is validated by
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>
Gets the length of text 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. 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);
}
}