Multiple lines of text box character number length verification solution!

Source: Internet
Author: User
Tags interface
Resolution | text box | text box in asp.net system often encounter validation text box length, such as the database is varchar (50), then it is best to control the interface to enter only 50 characters, this validation in some cases is essential:
Use the Length property for a single-line text box, this method is very simple, but there is a problem, is that the Chinese characters it is only a single character, while in the database is two characters, so like the above 50 characters, if you use LENGTH=50, if the user input 50 Chinese text, then the system will error , so you can use LENGTH=25 to control the maximum probability, which also reduces the number of words in English characters. This is also very important, as long as we design the database, we should need to enlarge the number of the number of times it can be.

The Length property for multiline text boxes does not work, so use the following scenario:
<script language= "JavaScript" >
<!--
String.prototype.len=function () {
Return This.replace (/[^\x00-\xff]/g, "* *"). Length;
}
function checklength (source, arguments)
{
var validstrlength=50;
if (arguments. Value.len () <=validstrlength)
Arguments. IsValid = true;
Else
Arguments. IsValid = false;
}
-->
</script>

Use the above script in the interface, and then in the need to verify the place, plus CustomValidator validation control, the ClientValidationFunction attribute is specified as "Checklength", this method is the above client function, the function of the var validstrlength=50; Refers to the number of characters to validate. To illustrate, the number of characters here will automatically distinguish between Chinese characters, a Chinese character is automatically recorded as two characters, so you do not need to be like a single-line text box, set to half the total number of characters to control.

OK, through the above settings, you can see the effect of being controlled!!
Good luck!



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.