[RESOLVED]Numeric range checking for textbox

i have wrote a function as a result user will not be to enter more than 70 and less than 1 but i want to incorporate decimal place checking. if user enter decimal value then i will allow him to enter 2 digit after decimal. so see my code and guide what i
need to add. thanks

$("input[id*='txtWeight']").keyup(function () {
        if (isNaN($.trim($("input[id*='txtWeight']").val()))) {
            $("input[id*='txtWeight']").val('5');
        }
        else {
            if (parseFloat($.trim($("input[id*='txtWeight']").val())) < 1) {
                $("input[id*='txtWeight']").val('5');
            }
            else if (parseFloat($.trim($("input[id*='txtWeight']").val())) > 70) {
                $("input[id*='txtWeight']").val('5');
            }
        }
    });

You can use the below modified code

<script>
        $(document).ready(function () {
            $("input[id*='txtWeight']").keyup(function (e) {
                if (isNaN($.trim($("input[id*='txtWeight']").val()))) {
                    $("input[id*='txtWeight']").val('5');
                }
                else {
                    if (parseFloat($.trim($("input[id*='txtWeight']").val())) < 1) {
                        $("input[id*='txtWeight']").val('5');
                    }
                    else if (parseFloat($.trim($("input[id*='txtWeight']").val())) > 70) {
                        $("input[id*='txtWeight']").val('5');
                    }
                }
                //Code to restrict decimal number of two places
                $("input[id*='txtWeight']").keypress(function (e) {
                    var character = String.fromCharCode(e.keyCode)
                    var newValue = this.value + character;
                    if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
                        e.preventDefault();
                        return false;
                    }
                });
            });
        });
    </script>

Complete Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <script>
        $(document).ready(function () {
            $("input[id*='txtWeight']").keyup(function (e) {
                if (isNaN($.trim($("input[id*='txtWeight']").val()))) {
                    $("input[id*='txtWeight']").val('5');
                }
                else {
                    if (parseFloat($.trim($("input[id*='txtWeight']").val())) < 1) {
                        $("input[id*='txtWeight']").val('5');
                    }
                    else if (parseFloat($.trim($("input[id*='txtWeight']").val())) > 70) {
                        $("input[id*='txtWeight']").val('5');
                    }
                }
                //Code to restrict decimal number of two places
                $("input[id*='txtWeight']").keypress(function (e) {
                    var character = String.fromCharCode(e.keyCode)
                    var newValue = this.value + character;
                    if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
                        e.preventDefault();
                        return false;
                    }
                });
            });
        });
    </script>
</head>
<body>
<input type="textbox" id="txtWeight" />
</body>
</html>

SourceURL

EDIT:Updated the Demo

script has problem. it is not allowing backspace and other valid keys

You need to change this 

               //Code to restrict decimal number of two places
                $("input[id*='txtWeight']").keypress(function (e) {
                    var character = String.fromCharCode(e.keyCode)
                    var newValue = this.value + character;
                    if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
                        e.preventDefault();
                        return false;
                    }
                });

So if you need to have characters other than numeric then simply add them as

var character = String.fromCharCode(e.keyCode)
if (character != 'X' && character != 'Y' ...)
{
   rest of code
}

or instead of string values from String.fromCharCode() use e.keyCode 

if (e.keyCode != 123 && e.keyCode != 345 ...)
{
   rest of code
}

just check the demo url and then u can see it is not taking delete key press or backspace. if i enter 5 then after that i can not enter another 5.

i want user will be able to enter 1 as min value and 70 as max value. 2 place decimal will be allowed. your script is not working. triple check it.

mou_inn

if i enter 5 then after that i can not enter another 5.

I am able to enter 5 and then again 5 (value is 55). I’m not able to enter 555 because your script resets it to "5".

Your script is following

            if (parseFloat($.trim($("input[id*='txtWeight']").val())) < 1) {
                $("input[id*='txtWeight']").val('5');
            }
            else if (parseFloat($.trim($("input[id*='txtWeight']").val())) > 70) {
                $("input[id*='txtWeight']").val('5');
            }

when you clicked 5" the value – neither <1 nor >70. So the code does not thing 

when you clicked "5" again – 55 neither <1 nor >70. So the code does not thing 

and when you finally clicked again "5" - the val() becomes "555" which is >70 so you reset it to "5"

$("input[id*='txtWeight']").val('5');

Leave a Reply