2011-10-29 12 views
1

ユーザが4桁の数字を入力した後に、(XXXX)の形式で1年間有効であることを確認したい。ユーザーが4桁の数字を入力すると、その値を使用したいajax呼び出し。外出先での入力検証

<h6>What year was the car manufactured</h6> 
<input name="car_year" type="text"> 
<div class="car_make_error car_year_error">Car Year Error</div> 

jqueryの:

$("input[name='car_year']").change(function() { 
    validate to make sure 4 digits are in the proper range and get the value of the 
    4 digits 
} 

私は、キーを使用することができます知っているが、私は任意の助けをいただければ幸い方法を正確に確認されませんでした。私はこのコードを使用しました

EDIT

:あなたは正しい軌道に乗って、すでにだったよう

$(car_year).keyup(function() { 
    var year = $(this).attr('value'); 
    $('.car_year').html(year); 
}); 

答えて

2

が見えます。これはあなたが必要とするものですか?

$(function() { 

    var LOWER_RANGE = 2000; 
    var UPPER_RANGE = 2020; 

    var $carYearInput = $('#car_year'); 

    $carYearInput.keyup(function() { 
     var value = $carYearInput.val(); 

     // Check that we were given 4 numbers 
     if(value.match(/^[0-9]{4}$/)) 
     { 
      // Convert the value to an int 
      var year = parseInt(value, 10); 

      // Check that the year is in range 
      if(year > LOWER_RANGE && year < UPPER_RANGE) 
      { 
       alert(year); 
      } 
     } 
    }); 

}); 
+0

ひどい、あなたは人々が将来的に年2過去の千年、あるいは7を入力する場合を除き。 –

+0

@Kolinkええ、私の悪い、私はあなたがコメントしていると同時にそのビットを追加していた。 – attack

+0

それは私に十分に見えるもの.... – killkrazy

1

このような何か:

$('#car_year').keyup(function() { 
    var year = parseInt($(this).attr('value')); // parseInt forces the value to int 
    var min = 1965; 
    var max = 2011; 
    if (year <= max && year >= min) { 
     // Will only go in here if it is an four digit integer between min and max 
     $('#result').html('Yeah, looks ok!'); 
    } else { 
     $('#result').html('Not a proper year...'); 
    } 
}); 

そして...