JS input text box can only enter a number or cannot enter a number

Source: Internet
Author: User

Description

1 can only enter 0 to 9 and decimal points
2 can only paste the number
3 can't drag content in
4 prohibit the use of input method

With the above, we can start.

Example (compatible IE, Firefox and other browsers)

The code is as follows Copy Code

<input
onkeypress= "Return event.keycode>=48&&event.keycode<=57| | Event.keycode==46 "
Onpaste= "Return!clipboarddata.getdata (' text '). Match (/d/)"
Ondragenter= "return False"
Style= "Ime-mode:disabled"
>

Cases

The code is as follows Copy Code

<input name= "username" type= "text" onkeyup= "Value=this.value.replace (/d+/g, ')" >

Cases

The code is as follows Copy Code

<script language=javascript>

function Onlynum ()

{

if (!) ( EVENT.KEYCODE==46) &&! (event.keycode==8) &&! (event.keycode==37) &&! (event.keycode==39))

if (!) ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))

Event.returnvalue=false;

}

</script><input onkeydown= "Onlynum ();" Style= "ime-mode:disabled>


Cases

You can only enter a number to lose a decimal point.

The code is as follows Copy Code
<input onkeyup= "if (isNaN (value)) ExecCommand (' Undo ') ' onafterpaste= ' if (isNaN (value)) ExecCommand (' Undo ') >
<input name=txt1 onchange= "if (/d/.test (this.value)) {alert (' can only enter numbers '); this.value= ';} ' >

Cases

The code is as follows Copy Code

function CheckNumber (e)
{
    var keynum;
    var Keychar;
    var Numcheck;
 
    if (window.event) {//ie
        keynum = e.ke Ycode;
      } else if (E.which) {//! IE
        keynum = E.which;
      }
 
    Keychar = String.fromCharCode (keynum);//Convert keyboard code to actual character
    Numcheck =/d/;
 
    return numcheck.test (Keychar);//Return!numcheck.test (Keychar); Cannot enter kanji
}

call method

<input type= "text" onkeypress= "return CheckNumber (Event)" value= "0"/>

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.