0

i would like to seek some help....my problem is my script restricts everything but numbers...i want to do is...i can type double numbers...like 1.232....i dont want to restrict the point(.)...can anyone help me do that?

current code:

<input autocomplete="off" class="search_form_input" name="optA1" id="optA1" onkeypress="return isnumeric(event)" type="text" onchange="optTotal1()" />

script code:

<script type="application/javascript">

  function isnumeric(evt)
      {
         var CharacterCode = (evt.which) ? evt.which : event.keyCode

         if (CharacterCode > 31 && (CharacterCode < 48 || CharacterCode > 57))
            return false;

         return true;
      }

</script>
Ben
  • 1,434
  • 14
  • 23
user3311499
  • 69
  • 1
  • 1
  • 11
  • 1
    add `alert(CharacterCode);` after you define it, refresh your page, type a `.` in the input, record the number, add `&& CharacterCode != X` to your if statement where X is your recorded number, remove your `alert`. – cmorrissey Feb 19 '14 at 16:37
  • Not sure, but your code allows me to type more than just digits. – putvande Feb 19 '14 at 16:45
  • maybe look at some suggestions here: http://stackoverflow.com/questions/2808184/ – HJ05 Feb 19 '14 at 17:01

2 Answers2

1

Something like:

$('input').on('keydown', function(e) {
    var dot = $(this).val().indexOf('.');
    if((e.which >= 48 && e.which <= 57) ||
       (e.which >= 96 && e.which <= 105) ||
        ((e.which == 190 || e.which == 110) && dot == -1)
        ) return true;
    return false;
});

This will also check your numpad and if you already have a . in your value.

Fiddle

putvande
  • 15,068
  • 3
  • 34
  • 50
-1

I'd consider a regexp:

var x=document.getElementById("optA1").value; return /[\d\.]+/.test(x);

Or you could pass the value through the function and use it that way.

Alex L
  • 470
  • 6
  • 15