Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

on one textbox i only want to enter numbers and decimal points. what i need is there should be only numbers , only one decimal points , back space allowd. no other special chars,no alphabets.


Can someone have any example.

JavaScript
function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }


i tried this on keypress on textbox but this allows only numbers not decimal poitns
Posted
Updated 25-Mar-22 1:50am

Try this

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        function isNumberKey(evt, obj) {

            var charCode = (evt.which) ? evt.which : event.keyCode
            var value = obj.value;
            var dotcontains = value.indexOf(".") != -1;
            if (dotcontains)
                if (charCode == 46) return false;
            if (charCode == 46) return true;
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }



    </script>
</head>
<body>
    <form id="frm" runat="server">
    <asp:TextBox ID="txt" onkeypress="return isNumberKey(event,this)" runat="server"></asp:TextBox>
    </form>
</body>
</html>
 
Share this answer
 
XML
<HTML>
<HEAD>
    <script type="text/Javascript">
function checkDec(el){
 var ex = /^[0-9]+\.?[0-9]*$/;
 if(ex.test(el.value)==false){
   el.value = el.value.substring(0,el.value.length - 1);
  }
}
</script>  </HEAD>   <BODY>
 <input type="text" id="" onkeyup="checkDec(this);" />
</BODY>
</HTML>
 
Share this answer
 
v2
C#
function validCheck(e) {
            var keyCode = (e.which) ? e.which : e.keyCode;
            if ((keyCode >= 48 && keyCode <= 57) || (keyCode == 8))
                return true;
            else if (keyCode == 46) {
                var curVal = document.activeElement.value;
                if (curVal != null && curVal.trim().indexOf('.') == -1)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
 
Share this answer
 
function validatenumerics(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
//comparing pressed keycodes

if (keycode > 31 && (keycode < 48 || keycode > 57) && keycode != 46) {
alert(" You can enter only characters 0 to 9 ");
return false;
}
else return true;
}
 
Share this answer
 
function isNumber(evt) {
evt = (evt) ? evt : window.event;

var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode &lt; 46 || charCode > 57 ) ) {
return false;
}
return true;
}
 
Share this answer
 
v2
Comments
CHill60 26-Feb-19 3:58am    
This is just a code-dump, not that much different to the other 4 code dumps posted here. It's a good idea to explain why your solution is better than the other 4.
function isNumberDecimal(evt, element) {
debugger;
var charCode = (evt.which) ? evt.which : event.keyCode

if (
(charCode != 46 || $(element).val().indexOf('.') != -1) &&
(charCode < 48 || charCode > 57))
return false;

return true;
}
 
Share this answer
 
Comments
CHill60 25-Mar-22 8:30am    
I can't believe I am repeating a comment on this thread from 2 years ago...
"This is just a code-dump, not that much different to the other 4 code dumps posted here. It's a good idea to explain why your solution is better than the other 4."
Except there are now 5.
Make sure you are bringing something new to any thread you answer and don't just dump a load of unformatted code - give some commentary

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900