Two methods to distinguish Chinese and English characters (Regular Expression and charCodeAt ())

Source: Internet
Author: User

For example, the Vanadium Form Verification plug-in I previously introduced this time, because it does not contain 2 Characters in E text, you have to expand it to distinguish Chinese and English characters. this article introduces two methods to distinguish Chinese and English characters: Regular Expression and charCodeAt.
<! Doctype html> <ptml> <pead> <meta charset = "gb2312"> <title> two methods to distinguish Chinese and English characters: Regular Expressions and charCodeAt () @ Mr. think </title> <style>/* reset css */body {font-size: 0.8em; letter-spacing: 1px; font-family: \ 5fae \ 8f6f \ 96c5 \ 9ed1; line-height: 1.8em} div, h2, p, fieldset, legend, form, textarea, span, em, sub {margin: 0; padding: 0} input {font: 12px/1.5 tahoma, arial, sans-serif; vertical-align: middle} h1 {font-size: 1em; font-weight: normal} h1 a {background: #047; padding: 2px 3px; color: # fff; text-decoration: none} h1 a: hover {background: # a40000; color: # fff; text-decoration: underline} h3 {color: #888; font-weight: bold; font-size: 1em; margin: 1em auto; position: relative}/* demo css */fieldset {padding: 20px; border: 1px solid # ccc; width: 720px} fieldset legend {background: # a40000; color: # fff; text-align: center; padding: 0 8px; margin-left: 25px} fieldset label {display: block; padding-left: 25px; line-height: 40px} fieldset label input {padding: 2px 3px; border: 1px solid #888; width: 200px; height: 16px} fieldset label input: focus {border: 1px solid blue} </style> <meta name = "author" content = "Mr. think qingbird "/> <meta name =" keywords "content =" Mr. think, front-end development, WEB front-end, front-end technology, front-end development, WEB front-end development, user experience, Website planning, website optimization, qingbird, javascript, jQuery, css, xhtml, html, UE, SEO, Mr. think blog, qingbird blog, PHP enthusiast, Bluebirdsky "/> <meta name =" description "content =" Mr. think's blog, named qingbird, is now focused on WEB Front-end development and is also a PHP enthusiast. love thinking, a little code cleanliness, raw crab legs, love meat. here is where I record knowledge and things in my life. "/> <link rel =" pingback "href =" http://mrthink.net/xmlrpc.php "/> <link rel =" alternate "type =" application/rss + xml "title =" Mr. think blog RSS Feed "href =" http://mrthink.net/feed/ "/> </pead> <body> Mr. think's personal blog @ focus on front-end technology, love PHP, advocate simple life. return to the Article Page: two methods to distinguish Chinese and English characters: Regular Expression and charCodeAt () method @ Mr. think <fieldset> <legend> enter characters in the following form and click the outer area of the form. </legend> <label for = "regexp"> regular expression: <input id = "regexp" name = "regexp"/> </label> <label for = "charcodeat"> using the charCodeAt () method: <input id = "charcodeat" name = "charcodeat"/> </label> </fieldset> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Regular Expressions are undoubtedly the most powerful method for judging various conditions. They have been studied recently. Although boring, they still have fun. it is easy to use it to determine a double-byte Chinese character. judge Chinese characters, Mr. think is also highly recommended for this method, which is simple and highly efficient.
The core code is as follows:
Copy codeThe Code is as follows:
RegExpForm. onblur = function (){
EntryVal = this. value;
EntryLen = entryVal. length;
CnChar = entryVal. match (/[^ \ x00-\ x80]/g); // use the match method to retrieve Chinese characters and return an array containing Chinese Characters
EntryLen + = cnChar. length; // calculate the actual character length
}

The second method is to use the charCodeAt () method to determine the Unicode code value. If it is greater than 255, It is a Chinese character (why? Please Google). Mr. Think does not recommend this method. Its execution efficiency is not as high as regular expressions, and it is not as simple as regular expressions.
The core code is as follows:
Copy codeThe Code is as follows:
EntryVal = this. value;
EntryLen = entryVal. length;
For (var I = 0; I <entryVal. length; I ++ ){
If (entryVal. charCodeAt (I)> 255) {// traverses and judges the Unicode code of each character in the string. if it is greater than 255, It is Chinese.
CnArr. push (entryVal [I]); // insert qualified values to the Chinese character array
// Pay attention to a small bug. push is to add one or more elements to the end of the array and return a new length. If it is not refreshed, repeated blur will accumulate character values.
}
}
EntryLen + = cnArr. length;

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.