Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting for numeric values but not allowing - symbol in text box.

i need proper code for allow negative symbol & numeric value

What I have tried:

<script type="text/javascript">
       function numericdecneg(evt) {
           evt = (evt) ? evt : window.event;
           var charcode = (evt.which) ? evt.which : evt.keyCode;
           if (charcode != 109 && charcode > 31 && (charcode < 48 || charcode > 57) && charcode != 46) {
               return false;
           return true;

Updated 12-Jan-17 3:42am
[no name] 11-Jan-17 9:43am
So add the character code for the - sign to your if.
Richard MacCutchan 11-Jan-17 10:02am
Use proper character constants instead of numbers, so your code is easy to read; and less likely to contain mistakes.
Richard Deeming 11-Jan-17 11:55am
Forget Javascript - use <input type="number" ... /> instead. Browser support[^] is almost universal - only IE9 and earlier and Opera Mini don't support it.

1 solution

No js needed
<input type="number" name="quantity" min="-9999" max="-1">

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