js verification or decimal number

Use a regular expression match is a number, directly on the demo

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>checkNumber</title>
</head>

<body style="background-color: aliceblue;">
    <div id="main" style="margin: 0 auto; text-align: center; ">
        <form method="POST" action=""
            style="width: 50% ; height: 50%; text-align: center;margin: 0 auto;  padding-top: 20%;">
            <label>請輸入判斷的數字:</label><input type="text" id="amount" name="amount"
                onblur="checkAmount('amount','hint')" /><br />
            <p><span name="hint" id="hint" style="color: red"></span></p>
        </form>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    $(function () {
        //設置div高度
        $("#main").css("height", $(document).height);
        $("#main " ) .css ( " width " , $ (Document) .width); 
    }); 

    function notIntOrDecimal (text) {
         // check (decimal / numeric) regex 
        the let pattern =  / ^ [0-9] + (. 1} {[0-9] + [.]) $ {0,1} / ;
         IF (pattern.test (text)) {
             return  to false ; 
        } the else {
             return  to true ; 
        } 
    } 

    function checkAmount (the objName, hintName ) { 
        the let judgeVal = $ ( " # " + The objName) .val ();
         IF (judgeVal ! ==  ''  && notIntOrDecimal (judgeVal)) { 
            $ ( " # "  + hintName) .html ( " Please enter an integer or decimal! " ); 
            $ ( " # "  + the objName) .focus (); 
        } the else { 
            $ ( " # "  + hintName) .html ( " the verification is successful! " ); 
        } 
    } 
</ Script > 

</ HTML >

 

Guess you like

Origin www.cnblogs.com/ccx-10544/p/11591918.html