Input uses JavaScript to restrict entering numbers with decimals

Source: Internet
Author: User

Title, a lot of online search is not very good to achieve. My implementation needs are as follows:

1, if the input number without a decimal point, then automatically add two decimal places, such as: Enter 5, replace with 5.00

2, enter 5., replace with 5.00

3, enter 5.1, replace with 5.10

4, input non-digital, automatic emptying

The JavaScript code is as follows:

//limit input numbers, only two-bitfunction Checknum (obj) {//Check whether a non-numeric value    if(IsNaN (Obj.value)) {Obj.value="0.00"; return; }    if(obj! =NULL) {        //Auto-fill 0 without decimals        if(Obj.value.indexOf ('.') == -1) {            if(Obj.value = ="") {Obj.value="0.00"; } Else{Obj.value+=". XX"; }        }        if(Obj.value.toString (). Split (".")[1].length <2) {            if(Obj.value.toString (). Split (".")[1].length = =0) {Obj.value+="xx"; }            if(Obj.value.toString (). Split (".")[1].length = =1) {Obj.value+="0"; }        }        //check for two-bit after decimal point        if(Obj.value.toString (). Split ("."). length >1&& obj.value.toString (). Split (".")[1].length >2) {Obj.value= Obj.value.substring (0, Obj.value.indexOf ('.') +3); }}}function Checknumforenter (obj) {//Enter event intercept    if(Event. keycode = = -) {         This. Checknum (obj); }}

Here's how to use it:

<asp:textbox id="txtb_minamount" runat="server" width= " 200px  "onblur="checknum (this)" onkeypress="checknumforenter ( This)"></asp:TextBox>

Note that you need to intercept onblur,onkeypress two events.

Input uses JavaScript to restrict entering numbers with decimals

Related Article

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.