Search code examples
javascriptjqueryjquery-effectstypeof

run $.ajax() when end typed with format specified. how is it?


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.


Solution

  • 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
    }