Regular Expression to validate number with one decimal point in jQuery

Last Reply 29 days ago By dharmendr

Posted 29 days ago

How to validate using Regular expression?

i wanted to validate the meter reading.

Valid value: 1234.0 123.5 51234.9

Invalid value: 1234 123 1235.99 1234.80

Posted 29 days ago

Hi chetan,

Check this example. Now please take its reference and correct your code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtMeterReading').keyup(function () {
                if ($(this).val().length > 0) {
                    var regex = /((\d+)(\.\d{1}))$/;
                    if (regex.test($(this).val())) {
                        $('#msg').html("Valid");
                    } else {
                        $('#msg').html("Invalid");
                    }
                } else {
                    $('#msg').html("");
                }
            });
        });
    </script>
</head>
<body>
    <input type="text" id="txtMeterReading" />
    <span id="msg" style="color: Red;"></span>
</body>
</html>

Demo