How to use Pure JS code to determine how many Chinese characters in a string (super simple practical) _javascript skills

Source: Internet
Author: User

In the site development, often simple use of JS code to determine the number of characters in the string of features. Today, a small compilation of time to share the implementation code. No more nonsense to say, directly to everyone paste code.

$ ("form"). Submit (function () {
var content = Editor.getcontenttxt ();
var sum = 0;
re =/[\u4e00-\u9fa5]/g; Test the regular
if (content) {if (re.test) (content) of
Chinese characters//use regular to determine whether there is an
content.match (re). Length <= 10) {//Return the number of Chinese
$.dialog.tips ("Post body can not be less than 10 characters!") ");
return false;
}
else {
var $submit = $ ("input[type= ' Submit ']"). attr ("Disabled", true);
settimeout (function () {$submit. attr ("disabled", false)}, 5000);
return true;
}
else {
$.dialog.tips ("Post body cannot be less than 10 characters!") ");
return false;
}
}
else {
$.dialog.tips ("Post body cannot be less than 10 characters!") ");
return false;
}
});

OK, the above code is JS judge string how many Chinese characters to achieve the method.

Ps:js to determine the length of the input string (Chinese characters are two characters, alphanumeric is one)

Chinese characters make up 2 characters in the database, and if the input character exceeds the length of the database table field, an error occurs, so you need to judge it in the foreground. There are two ways of judging:

Method one: Using regular expressions, the code is as follows:

Function Getbytelen (val) {
var len = 0;
for (var i = 0; i < val.length i++) {
var a = Val.charat (i);
if (A.match (/[^\x00-\xff]/ig)!= null) 
{
len + + 2;
}
else
{
Len + = 1;
}
}
return len;
}

Method Two: Use character Unicode to judge: The method is as follows:

Function Getbytelen (val) {
var len = 0;
for (var i = 0; i < val.length i++) {
var length = val.charcodeat (i);
if (length>=0&&length<=128)
{
Len + = 1;
}
else
{
Len + = 2;
}
}
return len;
}

The above is a small set to introduce the use of pure JS code to determine how many Chinese characters to achieve the method (super simple and practical), I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone.

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.