JavaScript form validation, when entering Chinese, the character length is 2

Source: Internet
Author: User

Gets the length of the character in the input box for form validation, and when the input is in English, the character length is 1, and when the Chinese is entered, the character length is 2.

The number of characters is 4~16 bit,

One Chinese to two English.

On the Code

1Btn.addeventlistener (' Click ', ov,false);2 3             functionov () {4                 varInputvalue =Text.value.trim ();5                 //remove the first and last space6                 if(Countlength (Inputvalue) < 4 | | countlength (inputvalue) > 16) {7                     //determine if the length is between 4-168pro.innerhtml = ' name format error ';9Pro.classname = ' Pro Warn ';TenText.classname = ' warn '; One}Else{ Apro.innerhtml = ' name format correct '; -Pro.classname = ' Pro Cor '; -Text.classname = ' Cor '; the                 } -             } -  -             functioncountlength (str) { +                 varInputlength = 0; -                 //give a variable to record the length +                  for(vari = 0; i < str.length; i++) { A                     varCountcode =str.charcodeat (i); at                     //returns the Unicode encoding of the character at the specified position -                     //determine if the ASCII code is ASCII, the first 128 characters of the Unicode code -                     if(countcode >= 0 && countcode <= 128) { -Inputlength + +; -}Else{ -Inputlength +=2; in                         //if it is an extension code, then one time +2 -                     } to                 } +                 returninputlength; -}

JavaScript form validation, when entering Chinese, the character length is 2

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.