In the ASP.NET system, you will often encounter the length of the verification text, such as VARCHAR (50) in the database, so it is best to control only 50 characters on the interface, this verification is essential in some cases. :
For a single line text box, use the length attribute, this method is simple, but there is a problem, it is Chinese characters that only records only one character, and two characters in the database, so it is like the above 50 characters, if used Length = 50, if the user enters 50 Chinese characters, then the system will report an error, so length = 25 can be used to control the maximum possibility, which also reduces the word number of English characters. This is also important, as long as we are in designing the database, you can magnify the number of numbers that should be needed.
It doesn't work for the Length attribute of multi-line text boxes, so the following scenarios should be used:
String.Prototype.len = function () {
Return this.Replace (/ [^ / X00- / XFF] / g, "**"). Length;
}
Function Checklength (Source, Arguments)
{
Var validstrlength = 50;
IF (arguments.value.len () <= validstrlength)
arguments.issalid = true;
Else
arguments.issalid = false;
}
// -> script> Using the above script on the interface, then add the CustomValidator verification control in the area that needs to be verified, and specify the ClientValidationFunction property as "Checklength", this method is the above client function, in the function VAR validstrlength = 50; means the number of characters to be verified. To explain, the number of characters here is automatically distinguished, and a Chinese character will automatically record two characters, so it is not necessary to set it to half of the total character number. Ok, pass the above settings, you can see the controlled effect! ! Good luck!