Javascript amount text box implementation code

Source: Internet
Author: User

Case 1: Press enter to implement Tab jump.
In response to the onKeyDown event in the text box, window. event. keyCode gets the keyCode clicked by the user.
(*) The keyCode and ASCII are not exactly the same. The ASCII value of the primary and keypad 1 is the same, but the keyCode is different. The keyCode of the carriage return is 13, and the keyCode of the Tab is 9.
<Body onkeydown = "if (window. event. keyCode = 13) {window. event. keyCode = 9;}">
Only a few keys can be replaced, but most of them cannot be replaced.
The keyboard code is different from the ASCII code.
KeyCode
8: Return key
46: delete
37-40: direction keys
48-57: number in the keypad
96-105: Number of the primary key area
110, 190: decimal point in the keypad and primary key areas
189 and 109: negative numbers in the keypad and primary key areas
13: Press ENTER
9: Tab is the stuff that moves the focus to the next text box.
Case 2: Amount text box
The text box related to the amount in the financial system has the following requirements:
Enter the amount text box without Chinese Input
You cannot enter a non-number.
The left alignment of the text box when the focus is in the text box; the right alignment of the text box when the focus is out of the text box, showing the kilobytes
Disable Input Method: style = "ime-mode: disabled" // compatible with FF and IE, and not compatible with Chrome
You can only enter valid values (k = 9). | (k = 13) | (k = 46) | (k = 8) | (k = 189) | (k = 109) | (k = 190) | (k = 110) | (k> = 48 & k <= 57) | (k> = 96 & k <= 105) | (k> = 37 & k <= 40 ). Onkeydown = "return numonKeyDown ()" Do not write onkeydown = "numonKeyDown ()" to distinguish between functions called by Event Response Functions and Event Response functions.
Do not paste (Great Tester), <input onpaste = "return false;", too violent, it should only prohibit the pasting of invalid values. In onpaste, use clipboardData. getData ('text') obtains the value in the clipboard and traverses each character to check whether it is a valid value. If all the values are valid, paste is allowed. If there is an invalid value, paste is prohibited. Charat、charcodeat( .doc)
When the focus is on, the left alignment has no sub-bits, and when the focus is not on, the right alignment has no sub-bits. This. style. textAlign = 'right'
For how to add a sub-location, see the remarks (*)
============================
(? = Exp) match the position before exp
(? = Exp) is also called a zero-width positive prediction predicate. It asserted that the position where it appears can match the expression exp. For example, \ B \ w + (? = Ing \ B), matching the front part of the word ending with ing (except for the ing part), such as searching for I'm singing while you're dancing. it will match sing and danc.
====================================
Copy codeThe Code is as follows:
Function f (){
Var txts = document. getElementsByTagName ('input ');
For (var I = 0; I <txts. length; I ++ ){
// Press enter to convert to tab
Txts [I]. onkeydown = function (){
If (window. event. keyCode = 13 ){
Window. event. keyCode = 9;
}
}
Txts [I]. onpaste = function (){
Var usrInput = clipboardData. getData ('text ');
Var k;
For (var I = 0; I <usrInput. length; I ++ ){
K = usrInput. charCodeAt (I );
// Only numbers of. Or 0-9 can be pasted. Refer to the ASCII character set.
If (k = 46) | (k> = 48 & k <= 56 )){
} Else {
Return false;
}
}
}
}
}

Sub-location (exercise code ):
Copy codeThe Code is as follows:
Function commafy (n)
{
Var re =/\ d {1, 3 }(? = (\ D {3}) + $)/g; // It must end with \ d {3}. It must start with 1-3 digits, but when it is replaced, it does not contain the ending \ d {3} digits. Var n1 = n. replace (/^ (\ d +) (\. \ d + )?) $/, Function (s, s1, s2) {return s1.replace (re, "$ &,") + s2 ;});
Return n1;
}
Function addQianFenWei (txtBox)
{
TxtBox. value = commafy (txtBox. value );
}
Function removeQianFenWei (txtBox)
{
TxtBox. value = txtBox. value. replace (/,/g, ""); // replace (',', '')
}

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function commafy (n)
{
Var re =/\ d {1, 3 }(? = (\ D {3}) + $)/g; // match one to three digits followed by three digits, but not the last three digits.
Var n1 = n. replace (/^ (\ d +) (\. \ d + )?) $/, Function (s, s1, s2) {return s1.replace (re, "$ &,") + s2 ;});
Return n1;
}
Function set0000w (){
Var objTxt = document. getElementById ('txt0000w ');
Var r = '';
For (var I = objTxt. value. length-1; I> = 0; I --){
If (I % 3 = 0 ){
R + = objTxt. value. charAt (I) + ',';
} Else {
R + = objTxt. value. charAt (I );
}
}
ObjTxt. value = r;
// ObjTxt. value = commafy (objTxt. value );
}
</Script>

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.