Home > Web Front-end > JS Tutorial > body text

JS determines whether the input is a number and how many decimal places to retain

php中世界最好的语言
Release: 2018-05-22 14:12:19
Original
3474 people have browsed it

This time I will bring you JS to determine whether the input is a number and how many decimals to retain. JS to determine whether the input is a number and how many decimals to retain. What are the precautions?. Here is a practical case. One Get up and take a look.

1. Verification method validationNumber(e, num) e represents the label object, num represents the number of decimal places to be retained

function validationNumber(e, num) {
      var regu = /^[0-9]+\.?[0-9]*$/;
      if (e.value != "") {
        if (!regu.test(e.value)) {
          alert("请输入正确的数字");
          e.value = e.value.substring(0, e.value.length - 1);
          e.focus();
        } else {
          if (num == 0) {
            if (e.value.indexOf('.') > -1) {
              e.value = e.value.substring(0, e.value.length - 1);
              e.focus();
            }
          }
          if (e.value.indexOf('.') > -1) {
            if (e.value.split('.')[1].length > num) {
              e.value = e.value.substring(0, e.value.length - 1);
              e.focus();
            }
          }
        }
      }
    }
Copy after login

2. Verification integer

<asp:TextBox ID="txtNg" name="txtNg" runat="server" Height="16px" Width="98px" Font-Size="9pt"
   CssClass="EditTextBox" onpropertychange="validationNumber(this,0)"></asp:TextBox>
Copy after login

3. One digit is reserved Decimal

<asp:TextBox ID="txtChglinecost" name="txtChglinecost" runat="server" Height="16px" Width="98px" Font-Size="9pt" 
   CssClass="EditTextBox" onpropertychange="validationNumber(this,1)"></asp:TextBox>
Copy after login

4. Keep two decimal places

<asp:TextBox ID="txtStdyr" name="txtStdyr" runat="server" Height="16px" Width="98px" Font-Size="9pt" 
   CssClass="EditTextBox" onpropertychange="validationNumber(this,2)"></asp:TextBox>
Copy after login

5. Keep three decimal places

Just put validationNumber(this,3) in the method Just change the second parameter to 3.

Keep four decimal places, keep five decimal places, and so on...

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

What are the precautions for Vue to use vux-ui custom form verification

Common in vue projects Detailed explanation of the steps for adapting the bottom position of the footer component

The above is the detailed content of JS determines whether the input is a number and how many decimal places to retain. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!