Javascript + Regular Expression to determine whether the input is a number

Source: Internet
Author: User

 

1 <SCRIPT>
2 function mygetkeycode (e ){
3 var code;
4 If (! E) var E = Window. event;
5 If (E. keycode ){
6 code = E. keycode;
7} else if (E. Which ){
8 code = E. Which;
9}
10 return code;
11}
12
13 //-2.36 onkeypress
14 function mychecknum (OBJ, E)
15 {
16 var code = mygetkeycode (E );
17 if (Code <45 response code> 57 response code = 47) & code! = 45 & code! = 9 & code! = 8) return false;
18 if (code = 46 & obj. value. indexof (".")>-1) return false;
19 Return true;
20}
21
22 // 325 onkeypress
23 function mycheckint (OBJ, E)
24 {
25 var code = mygetkeycode (E );
26 if (Code <48 response code> 57) & code! = 45 & code! = 9 & code! = 8) return false;
27 return true;
28}
29 </SCRIPT>
30 can only be an integer
31 <input type = "text" name = "txtname" onkeypress = "Return mycheckint (this, event)"> <br/>
32 decimal places
33 <input type = "text" name = "txtname" onkeypress = "Return mychecknum (this, event)"> <br/>
34

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.