Regular Expression to accept only 0 and 100 in TextBox using JavaScript

Waghmare
 
on Dec 07, 2021 02:34 AM
806 Views

Hi Team ,

I have a textbox which should accept only 0 or 100 in it.

Please suggest regular expression for this.

I have tried regular one but that expects numbers between 0-9.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 07, 2021 03:14 AM
on Dec 07, 2021 05:21 AM

Hi Waghmare,

Refer below code.

HTML

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        .error {
            color: red;
        }
    </style>
</head>
<body>
    <input type="text" id="txtNumber" />
    <br />
    <span id="lblError" style="color: red"></span>
    <br />
    <br />
    <input type="button" id="btnValidate" value="Submit" onclick="ValidateNumber()" />
    <script type="text/javascript">
        function ValidateNumber() {
            var number = document.getElementById("txtNumber").value;
            var lblError = document.getElementById("lblError");
            lblError.innerHTML = "";
            var expr = /^([0]|100)$/;
            if (!expr.test(number)) {
                lblError.innerHTML = "Only 0 or 100 allowed.";
            }
        }
    </script>
</body>
</html>

Demo