How to do a numerical check using jQuery?

How to do a numerical check using jQuery. I have to check my price field using jQuery. Any body knows, please share your knowledge with me :(

+3
source share
5 answers

If you are looking for form confirmation, you can see the validation plugin: http://docs.jquery.com/Plugins/Validation

However, if you just want to get the example code, it could be so simple:

$(function() {
  $('ref-to-input').blur(function() {
    if($(this).val().match(/[^\d]/)) {
      // invalid chars detected
    }
  });
});

, :

$(function() {
  $('ref-to-input').keyup(function() {
    $(this).val($(this).val().replace(/[^\d]/, ''));
  });
});

:

+5

jquery

http://www.bassistance.de/jquery-plugins/jquery-plugin-validation/

It contains all the checks, such as require, format, size, range, value

+1
source
function IsNumeric(sText){
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

Using:

if ( IsNumeric(my_number) )
//do some here...
+1
source

You just need to apply this method in jQuery, and you can check your text box to just accept the number.

function IsNumberKeyWithoutDecimal(element) {    
var value = $(element).val();
var regExp = "^\\d+$";
return value.match(regExp); 
}

Watch a working demo here

0
source

Source: https://habr.com/ru/post/1750966/


All Articles