Multi-line text box character length verification solution!

Source: Internet
Author: User

In ASP. for example, if the database is varchar (50), it is best to enter only 50 characters on the interface, this verification is required in some cases:

You can use the Length attribute for a single-line text box. This method is simple, but there is a problem that the Chinese character is only recorded as one character, and the database contains two characters, therefore, if the length is 50 and 50 characters are entered, the system reports an error. Therefore, length = 25 can be used to control the maximum possibility, this also reduces the number of characters in English characters. This is actually very important, as long as we increase the number of numbers that should be needed by a factor when designing the database.

The Length attribute of the multi-line text box does not work. Therefore, we need to use the following scheme:

<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 on the interface, and add the customvalidator verification control to verify the clientvalidationfunction attribute as "checklength". This method is the client function above, vaR validstrlength = 50; indicates the number of characters to be verified. It should be noted that the number of characters here will automatically distinguish between Chinese characters, and a Chinese character will be automatically recorded as two characters. Therefore, it is not required to be like a single-line text box, set it to half of the total number of characters.

Now, you can see the effect of the control through the above settings !!

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.