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

I am using this code to enter only numbers in a textbox and works perfectly.

<script type="text/javascript">
        function fnAllowNumeric() {
            if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8) {
                event.keyCode = 0;
                alert("Please Enter Number Only");
                return false;
            }
        }
</script>

Now on top of this I should allow minus sign to be entered at the start. Can anyone suggest me some solutions?
Posted

 
Share this answer
 
Try JavaScript isNaN() Function[^]
+++++[round 2]+++++
An example to adapt:
XML
<script>
function myFunction() {
    var a = document.getElementById("txt").value.trim();
    if (isNaN(a) || a == ""){
        alert("Not a valid number!");
    } else {
        alert("A valid number!");
    }
}
</script>

<input type="text" value="" id="txt">
<button onclick="myFunction()">Click me</button>
 
Share this answer
 
v2
Comments
vignesht50 1-Jun-15 7:41am    
I can validate for numbers alone, but I need to allow to input ' - ' sign too.
Peter Leow 1-Jun-15 8:05am    
See the added example code and adapt.

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