JS allows only numbers

Source: Internet
Author: User

OriginalArticleIn: http://www.cnblogs.com/ly5201314/archive/2009/03/04/1402993.html

It is well written and practical. You can modify the extended functions by yourself:

To solve the problem that only numbers can be entered, there is a lot of information on the Internet.
1. Input without a negative number
The problem of "Positive and Negative" is not solved here.
Because the "plus and minus signs" must appear at the front of the number, you must determine whether the cursor is at the top of the input text box.

<! -- // Call method: onkeydown = "digitinput (this, event);" function digitinput (El, Ev) {// 8: Return key, 46: delete, 37-40: direction keys // 48-57: numbers in the keypad, 96-105: numbers in the primary keyboard area // 110, 190: decimal places in the keypad area and the primary key area // 189, 109: the negative number var event = EV in the keypad and primary key areas | window. event; // get the event object var currentkey = event under IE and ff. charcode | event. keycode; // obtain the keyboard code in IE and FF // process the decimal point if (currentkey = 110 | currentkey = 190) {If (El. value. indexof (". ")> = 0) if (window. event) // ie event. re Turnvalue = false; // E. returnvalue = false; same effect. Else // Firefox Event. preventdefault ();} else if (currentkey! = 8 & currentkey! = 46 & (currentkey <37 | currentkey> 40) & (currentkey <48 || currentkey> 57) & (currentkey <96 | currentkey> 105 )) if (window. event) // ie event. returnvalue = false; // E. returnvalue = false; same effect. else // Firefox Event. preventdefault () ;}-->

2. Input with a negative number
to determine the input with a negative number, you must know the cursor position, that is, the negative sign is valid only when the cursor is at the frontend.

<! -- // Call method: onkeydown = "digitinput (this, event);" function digitinput (El, Ev) {// 8: Return key, 46: delete, 37-40: direction keys // 48-57: numbers in the keypad, 96-105: numbers in the primary keyboard area // 110, 190: decimal places in the keypad area and the primary key area // 189, 109: the negative number var event = EV in the keypad and primary key areas | window. event; // get the event object var currentkey = event under IE and ff. charcode | event. keycode; // obtain the keyboard code in IE and FF // process the decimal point if (currentkey = 110 | currentkey = 190) {If (El. value. indexof (". ")> = 0) if (window. event) // ie event. re Turnvalue = false; // E. returnvalue = false; same effect. else // Firefox Event. preventdefault ();} else // process if (currentkey = 189 | currentkey = 109) {If (getposition (EL)> 0 | el. value. indexof ("-")> = 0) if (window. event) // ie event. returnvalue = false; // E. returnvalue = false; same effect. else // Firefox Event. preventdefault ();} else if (currentkey! = 8 & currentkey! = 46 & (currentkey <37 | currentkey> 40) & (currentkey <48 || currentkey> 57) & (currentkey <96 | currentkey> 105 )) if (window. event) // ie event. returnvalue = false; // E. returnvalue = false; same effect. else // Firefox Event. preventdefault ();}/*** get the character position of the cursor * @ Param OBJ control to be processed. It supports text fields and input boxes * @ author hotleave */function getposition (OBJ) {var result = 0; If (obj. selectionstart) {// non-IE browser result = obj. selectionstart} else {// ie var RNG; If (obj. tagname = "textarea") {// if it is a text field RNG = event. srcelement. createTextRange (); RNG. movetopoint (event. x, event. y);} else {// input box RNG = document. selection. createRange ();} RNG. movestart ("character",-event. srcelement. value. length); Result = RNG. text. length;} return result;} -->

The getposition (OBJ) function is a common function used to obtain the cursor position.
To reduce input parameters, the event object can be obtained through the event:

 
VaR El = Window. event. srcelement | ev.tar get;

You only need to add the preceding statement to the first sentence of digitinput (El, Ev) and only one EV parameter is input. This reduces the input value.
Iii. Expansion
We can add an input parameter value to limit the allowed numeric conditions:
1: positive integers are allowed; 2. Positive decimals are allowed; 3. negative integers are allowed; 4. Negative decimals are allowed.
Of course, you can also pass in a regular expression.

 
Function onlynum () {If (! (Event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105) | (event. keycode = 8) // return | (event. keycode = 46) // del | (event. keycode = 27) // ESC | (event. keycode = 37) // left | (event. keycode = 39) // right | (event. keycode = 16) // shift | (event. keycode = 9) // tab) // consider the number key event on the keypad. returnvalue = false ;}

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.