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

1

6 Answers 6

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;
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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;
    }
};​

Comments

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.

Comments

0

// Jquery codes
$(document).ready(function () {
    $("#input-number").keydown(function (e) { 
        if((e.keyCode <= 57 && e.keyCode >= 48) || (e.keyCode <= 105 && e.keyCode >= 96) || e.keyCode == 8 ){
            console.log(e.keyCode)
        }else{
            return false
        }
    });
});
// Character numbers keyboard 48 - 57 && 96 - 105 backspace is 8
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input type="text" id="input-number">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.