[Problem] [classic problem] For JavaScript verification text box input, only half-width can be entered, not full-width

Source: Internet
Author: User

From:Http://hi.baidu.com/wujjtest/blog/item/91f5a57291c428158701b050.html

Solution 1:

<Input onkeyup = "value = value. replace (/[\ uff00-\ Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ uff00-\ Uffff]/g, '')">

I am daring to comment: I cannot convert symbols such as ",", and the letter is incomplete... You need to be considerate or transformed.

 

Solution 2:

Halfwidth and fullwidth conversion functions [JS] [conversion]

/*
****************************
* Halfwidth <=> fullwidth *
* [NB Alliance] *
* Writer yuping *
****************************
* Parameter description:
* STR: string to be converted
* Flag: flag. The flag is converted to full at half past zero. If the flag is not 0, it is switched to half.
* Return value type: String
****************************
*/
Function dbc2sbc (STR, flag ){
VaR I;
VaR result = ';
If (Str. Length <= 0) {alert ('string parameter error'); Return false ;}
For (I = 0; I <Str. length; I ++)
{Str1 = Str. charcodeat (I );
If (str1 <125 &&! Flag)
Result + = string. fromcharcode (Str. charcodeat (I) + 65248 );
Else
Result + = string. fromcharcode (Str. charcodeat (I)-65248 );
}
Return result;
}
// Example:
Alert (dbc2sbc ("aaabc", 0 ));
Document. Write (dbc2sbc ("abcdefg", 0 ))

<Input type = text value = "abcdefg" id = TXT> <input type = button value = "" onclick = TXT. value = dbc2sbc (txt. Value)>

I am brave enough to comment on it: The image is so flashy that everyone will be fond of it. Only letters can be converted. Funny and funny.

Solution 3:

<SCRIPT>
VaR STR = "2003104345510defghighij9"
VaR BSTR = "0123456789 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
VaR gstr = "0123456789 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
For (I = 0; I <Str. length; I ++)
{
BPOs = BSTR. indexof (Str. substr (I, 1 ))
If (BPOs! =-1)
{
Bword = BSTR. substr (BPOs, 1)
Gword = gstr. substr (BPOs, 1)
STR = Str. Replace (New Regexp (bword, "G"), gword)
}
}
Alert (STR)
</SCRIPT>

I am daring to comment: simple and effective. Also works. However, it takes time and effort to manually add a category.

Solution 4: (ultimate solution)

<SCRIPT>
// Assign the character you think is fullwidth to the array str1, and then you can determine the fullwidth and halfwidth.
VaR str1 = new array ("? ");
VaR str2 = "ABC ";
VaR Re =/[^ \ x00-\ X80] // or/[^ \ 0-\ 127]/decimal representation
// [^ A-Z] matches within the specified range
// Alert (str1.length );
For (VAR I = 0; I <str1.length; I ++)
{
If (Re. Test (str1 [I])
{
Alert ("full-width character found, character:" + str1 [I]);
}

}
</SCRIPT>

The aboveCodeVerification results can be achieved without listing them. Please verify...

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.