Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26668

Allow/validate integer, decimal, float values in jquery

I had a html field like below

<input type="text" name="price" class="validate_price" id="price"/>

So the field should accept only float, decimal, integer values from the input.

jQuery code

<script>
   function isFloat(value) 
             { 
                 return value!= "" && !isNaN(value) && Math.round(value) != value;
              }   
   $(document).ready(function(){
       $('.validate_price').focusout(function(){
             var value  = this.value;
             if (value)
                {
                   if (!isFloat(value) || !$.isNumeric(value))
                      alert('Value must be float or not');  
                }     

       });
   });
</script>

So from the above code, what all my intention is, I should allow user to enter integer values(1,2,2, etc.,), float values(1.0, 2.0, 3.0, 3.2, 3.5, etc.) and decimal values(2.152, 4.56, 5.000, 6.3256, 2.00 etc.,)

How to check/implement the above functionality ?

Upvotes: 3

Views: 31400

Answers (4)

Pradeep Kumar Das
Pradeep Kumar Das

Reputation: 891

 $("#Latitude, #Longitude, #Revenue").keydown(function(e) {
        debugger;
        var charCode = e.keyCode;
        if (charCode != 8) {
            alert($(this).val());
            if (!$.isNumeric($(this).val()+e.key)) {
                return false;
            }
        }

    return true;
    });

Upvotes: 0

jatinkumar patel
jatinkumar patel

Reputation: 2990

This will valid your all float numbers.It will also valid only single time negative sign('-') and dot(.) symbol.

function float_validation(event, value){
    if(event.which < 45 || event.which > 58 || event.which == 47 ) {
          return false;
            event.preventDefault();
        } // prevent if not number/dot

        if(event.which == 46 && value.indexOf('.') != -1) {
            return false;
            event.preventDefault();
        } // prevent if already dot

            if(event.which == 45 && value.indexOf('-') != -1) {
                return false;
            event.preventDefault();
        } // prevent if already dot

        if(event.which == 45 && value.length>0) {
            event.preventDefault();
        } // prevent if already -

    return true;

};


<input type="text" onkeypress="return float_validation(event, this.value)" class="form-control"/>

This will validate: -12.33 12.44 12

Upvotes: 1

Mike
Mike

Reputation: 24383

If you notice from the jQuery.isNumeric() manual manual, it also validates floats, so there is no need to include any other arguments. To validate a number, all you have to do is use jQuery.isNumeric():

if (!$.isNumeric(value)) {
    alert('Value must be numeric');
}

I made a quick jsFiddle for you: http://jsfiddle.net/guxz9/

Upvotes: 10

LeGEC
LeGEC

Reputation: 51860

Taken from a paragraph in MDN documentation on parseFloat :

var filterFloat = function (value) {
    if(/^\-?([0-9]+(\.[0-9]+)?|Infinity)$/
      .test(value))
      return Number(value);
  return NaN;
}

Upvotes: 1

Related Questions