-1

i want, when user typed, 0000/00/00(it is format date) after the end of typing run anything(example: AJAX CALL), how is it?

like:
if user typed: 2011/05/04 -> run: $.ajax()...
if user typed: 2011/05 -> not run: $.ajax()...
...

only thing for run $.ajax() is true type this format 0000/00/00.
I hope you understand my purpose.
With respect.

Me hdi
  • 1,454
  • 4
  • 19
  • 31

3 Answers3

3

HTML:

<input type="text" name="datefield" onchange="validateField(this)" />

JS:

function validateField() {
   var val = this.value;
   if (val.length < 10) {
      return;
   }
   if (!val.match(/\d\d\d\d\/\d\d\/\d\d/)) {
      return;
   }
   ... do ajax call here
}
Marc B
  • 340,537
  • 37
  • 382
  • 468
  • 1
    You have to be careful cause this will match "blah blah 2011/02/04" as well. You would need to do /^\d\d\d\d\/\d\d\/\d\d$/ if the only thing allowed in there is the date. – Bill Criswell Jul 19 '11 at 16:09
  • True enough, just a quick/dirty example. – Marc B Jul 19 '11 at 16:11
1
$("inputTextBoxSelector").keyup(function(e){
  if($(this).val().match(/\d\d\d\d\/\d\d\/\d\d/){
    $.ajax(....);
    return false;
  }
})
ShankarSangoli
  • 67,648
  • 11
  • 84
  • 121
0

You can do...

$('input').change(function(e) {
  var val = $(this).val();
  if ( val.length == 10 ) {
    if( val.match(/^\d{4}\/\d{2}\/\d{2}$/) ) {
      $.ajax();
    }
  }
});

You can also try keyup instead of change.

Bill Criswell
  • 28,428
  • 3
  • 69
  • 65