Nice programing

입력 한 숫자가 jquery의 숫자인지 확인

nicepro 2020. 11. 19. 22:02
반응형

입력 한 숫자가 jquery의 숫자인지 확인


textbox사용자가 숫자를 입력 하는 간단한 방법 이 있습니다.
jQuery에 isDigit사용자가 숫자 이외의 것을 입력하면 경고 상자를 표시 할 수 있는 기능이 있습니까?

필드에는 소수점도있을 수 있습니다.


정규식을 사용하는 것이 좋습니다.

var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;

var str = $('#myTextBox').val();
if(intRegex.test(str) || floatRegex.test(str)) {
   alert('I am a number');
   ...
}

또는 @Platinum Azure의 제안에 따라 단일 정규식으로 :

var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
var str = $('#myTextBox').val();
if(numberRegex.test(str)) {
   alert('I am a number');
   ...
}    

정규 표현식은 잊어 버리세요. JavaScript에는 다음과 같은 기능이 내장되어 있습니다 isNaN().

isNaN(123)           // false
isNaN(-1.23)         // false
isNaN(5-2)           // false
isNaN(0)             // false
isNaN("100")         // false
isNaN("Hello")       // true
isNaN("2005/12/12")  // true

다음과 같이 호출하십시오.

if (isNaN( $("#whatever").val() )) {
    // It isn't a number
} else {
    // It is a number
}

변수가 정수인지 확인하는 더 간단한 방법이 있습니다. $ .isNumeric () 함수를 사용할 수 있습니다. 예 :

$.isNumeric( 10 );     // true

이것은 true를 반환하지만 10 대신 문자열을 넣으면 false가됩니다.

이것이 당신에게 효과가 있기를 바랍니다.


다음 스크립트를 사용하여 값이 유효한 정수인지 여부를 확인할 수 있습니다.

  function myFunction() {
    var a = parseInt("10000000");
    if (!isNaN(a) && a <= 2147483647 && a >= -2147483647){
    alert("is integer"); 
    } else {
     alert("not integer"); 
    }
}

var yourfield = $('fieldname').val();

if($.isNumeric(yourfield)) { 
        console.log('IM A NUMBER');
} else { 
        console.log('not a number');
}

JQUERY 문서 :

https://api.jquery.com/jQuery.isNumeric/


Value validation wouldn't be a responsibility of jQuery. You can use pure JavaScript for this. Two ways that come to my mind are:

/^\d+$/.match(value)
Number(value) == value

With jQuery's validation plugin you could do something like this, assuming that the form is called form and the value to validate is called nrInput

$("form").validate({
            errorElement: "div",
            errorClass: "error-highlight",
            onblur: true,
            onsubmit: true,
            focusInvalid: true,
            rules:{
                'nrInput': {
                    number: true,
                    required: true
                }
            });

This also handles decimal values.


String.prototype.isNumeric = function() {
    var s = this.replace(',', '.').replace(/\s+/g, '');
return s == 0 || (s/s);
}

usage

'9.1'.isNumeric() -> 1
'0xabc'.isNumeric() -> 1
'10,1'.isNumeric() -> 1
'str'.isNumeric() -> NaN

jQuery is a set of JavaScript functions, right? So you could use JavaScript's regular expression support to validate the string. You can put this in a jQuery callback if you like, too, since those just take anonymously-declared function bodies and the functions are still JavaScript.

Link: http://www.regular-expressions.info/javascript.html


$(document).ready(function () {



    $("#cust_zip").keypress(function (e) {
        //var z = document.createUserForm.cust_mobile1.value;
        //alert(z);
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {

            $("#errmsgzip").html("Digits Only.").show().fadeOut(3000);
            return false;
        }
    });
});

참고URL : https://stackoverflow.com/questions/1272696/checking-if-number-entered-is-a-digit-in-jquery

반응형