asp.net maxlengthvalidator maximum length validation control code _ Practical Tips

Source: Internet
Author: User
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);
}
}
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.