如何使用jQuery检查一个值是否为数字

如何使用jQuery检查一个值是否为数字

给定一个输入元素,任务是使用jQuery检查输入的值是否为数字。jQuery $.isNumeric()方法是用来检查输入的数字是否为数字的。

$.isNumeric()方法:它用于检查给定的参数是否是一个数字值。如果是数值,则返回真,否则返回假。

语法:

$.isNumeric( argument )

例子1:这个例子使用jQuery .isNumeric()方法来检查输入的元素是否是数字的。

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to check whether a value is
        numeric or not in jQuery?
    </title>
      
    <script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
    </script>
</head>
  
<body style="text-align:center;">
      
    <h1 style = "color:green;" > 
        GeeksForGeeks 
    </h1> 
      
    <h3>
        How to check whether a value
        is numeric or not in jQuery?
    </h3>
      
    <hr>
      
    <form>
        <p>
            Enter any value: 
            <input style="text-align:center;" type="text">
        </p>
          
        <button type="button">Click to Check</button>
    </form>
    <hr>
      
    <script type="text/javascript">
        (document).ready(function() {
            ("button").click(function() {
                var inputVal = ("input").val();
                alert(.isNumeric(inputVal));
            });
        });
    </script>
</body>
  
</html>          

输出:

  • 在输入数值之前。
    如何使用jQuery检查一个值是否为数字?
  • 输入数值。
    如何使用jQuery检查一个值是否为数字?
  • 点击按钮后。
    如何使用jQuery检查一个值是否为数字?

例子2:这个例子使用jQuery .isNumeric()方法来检查输入的元素是否是数字的。

<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to check whether a value
        is numeric or not in jQuery?
    </title>
      
    <script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
    </script>
</head>
  
<body style="text-align:center;">
      
    <h1 style = "color:green;" > 
        GeeksForGeeks 
    </h1> 
      
    <h3>
        How to check whether a value 
        is numeric or not in jQuery?
    </h3>
    <hr>
      
    <form>
        <p>
            Enter any value: 
            <input style="text-align:center;" type="text">
        </p>
          
        <button type="button">Click to Check</button>
    </form>
    <hr>
      
    <h4></h4>
      
    <script type="text/javascript">
        (document).ready(function() {
            ("button").click(function() {
                var inputVal = ("input").val();
                var gfg =.isNumeric(inputVal);
                  
                if (gfg) {
                    ("h4").text("The Value Entered is Numeric");
                }
                else {
                    ("h4").text("The Value Entered is Not Numeric");
                }
            });
        });
    </script>
</body>
  
</html>  

输出:

  • 在输入数值之前。
    如何使用jQuery检查一个值是否为数字?
  • 输入数值后,点击按钮。
    如何使用jQuery检查一个值是否为数字?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程