2012-07-30 3 views
2

jquery.No 이외의 숫자를 확인하려고하면 다른 숫자를 내 함수가 작동하지만 0과 음수가 아닌지 작동하지 않습니다. 내 샘플 피들입니다.
Sample Fiddle
내 실수를 찾을 수 없습니다. 감사합니다. 당신이 정수에 대해 확인하고 이후jquery의 유효성 검사 번호

옳지 않아
+1

이이 작은 * *의 jQuery를 함께 할 수있는. –

답변

1

을 염려을 경우에 대한 DEMO하는 방법 (참고 : 오류 메시지가 OP 자신의)

$('#txtNumber').keyup(function() { 
    var val = $(this).val(), error =""; 
    $('#lblIntegerError').remove(); 
    if (isNaN(val)) error = "Value must be integer value." 
    else if (parseInt(val,10) != val || val<= 0) error = "Value must be non negative number and greater than zero"; 
    else return true; 
    $('#txtNumber').after('<label class="Error" id="lblIntegerError"><br/>'+error+'</label>'); 
    return false; 
}); 
+0

0보다 큰 음수가 아닌 _and_? _positive_을 의미합니까? – nnnnnn

+0

내 메시지가 아닙니다 ... – mplungjan

0
if (isNaN($('#txtColumn').val() <= 0)) 

..

당신은

var intVal = parseInt($('#txtColumn').val(), 10); // Or use Number() 

if(!isNaN(intVal) || intVal <= 0){ 
    return false; 
} 
+0

@Matt Lo : 그렇습니다. 감사합니다 :) –

+0

하지만 사용자가 유효한 정수 뒤에 잘못된 데이터를 입력하면 작동하지 않습니다. 예를 들어,'parseInt ("123.45abcdefg", 10)'은'123'을 반환합니다. 또한 기수를 지정하거나 (두 번째 매개 변수)'parseInt ("0x12")'는'18'을 반환하고'parseInt ("010")'는''를 반환합니다 ... – nnnnnn

+0

@nnnnnn : 베이스. 'parseInt ('010', 10)'또는'Number ('010')'를 사용하십시오. 'parseInt()'는 기본으로 8을 사용합니다. 그것에 대한 자세한 내용은 [이 질문에] (http://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug)를 참조하십시오. –

0

이 작동합니다 값을 정수로 캐스팅해야합니다

$('#txtNumber').keyup(function() { 
    var num = $(this).val(); 
    num = new Number(num); 
    if(!(num > 0)) 
     $('#txtNumber').after('<label class="Error" id="lblIntegerError"><br/>Value must be non negative number and greater than zero.</label>'); 
}); 

참고 :parseInt()은 잘못된 문자를 무시합니다. 첫 번째 문자가 숫자 만 Number()도 그들

0
$('#txtNumber').keyup(function() 
{ 
    $('#lblIntegerError').remove(); 
    if (!isNaN(new Number($('#txtNumber').val()))) 
    { 
     if (parseInt($('#txtNumber').val()) <=0) 
     { 
       $('#txtNumber').after('<label class="Error" id="lblIntegerError"><br/>Value must be non negative number and greater than zero.</label>'); 
      return false; 
     } 


    } 
    else 
    { 
      $('#txtNumber').after('<label class="Error" id="lblIntegerError"><br/>Value must be integer value.</label>'); 
      return false; 
     } 
});​ 
관련 문제