0

in my code i am restricting only numeric values into my input field for that i am using this code.

if($('.calculator #loan_Amount').val() != "") {
    var value = $('.calculator #loan_Amount').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    var intRegex = /^\d+$/;
    if(!intRegex.test(value)) {
        alert('Loan amount must be numeric.');
        return false;
    }
} 

but now if i enter the value 6500.25 it says the value has to be numeric it is not taking the decimal digit as numeric. is any body having idea regarding it

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
user2142786
  • 1,484
  • 9
  • 41
  • 74
  • http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all – run Nov 25 '13 at 05:53

5 Answers5

4

\d+ means one or more integers not floating number and hence it fails.

Using Regex

Try this \d+(\.\d*)* means

enter image description here

Using jQuery

Since you already use jquery, so better go with $.isNumeric()

if($('.calculator #loan_Amount').val() != "") {
   if(!$.isNmeric(this.value) {
        alert('Loan amount must be numeric.');
        return false;
    }
} 
Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

With jQuery Masked Input plugin, it is possible to ignore keys other than digits. It can also allow you to do custom digit grouping (aka thousand separator) without having user to hit comma keys. Have a look at the Demo tab on this page.

fahadash
  • 3,133
  • 1
  • 30
  • 59
0
$('.calculator #loan_Amount').keyup(function(){
    var iptext = $(this).val();
    if(!(iptext.match('^(0|[1-9][0-9]*)$')))
    {
        $(this).val('');
    }
});

this will accept only the neumeric value.

Shaik Rilwan
  • 365
  • 2
  • 8
0

Here is a solution which blocks all non numeric input from being entered into the text-field.

html

<input type="text" id="numbersOnly" />

javascript

var input = document.getElementById('numbersOnly');
input.onkeydown = function(e) {
    var k = e.which;
    /* numeric inputs can come from the keypad or the numeric row at the top */
    if ( (k < 48 || k > 57) && (k < 96 || k > 105)) {
        e.preventDefault();
        return false;
    }
};​
jforjs
  • 465
  • 1
  • 4
  • 11
0

You can use the JavaScript isNaN(value) method which returns true if value (Not a Number) Otherwise false

  if($('.calculator #loan_Amount').val() != "") {
    var value = $('.calculator #loan_Amount').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if(isNaN(value)) 
    {
        alert('Loan amount must be numeric.');
        return false;
    }
} 

Hope this Helps you.

Ajinder Singh
  • 532
  • 2
  • 8