1. 程式人生 > >非常好用的ajax表單驗證

非常好用的ajax表單驗證

<script type="text/javascript">

$(function(){

  $("#code").blur(function(){

        //   alert($(this).val());

  $.post("${pageContext.request.contextPath}/CodeServlet.as",{code:$(this).val()},

  function(data){

    if(data=="3"){     //data 就是從servlet返回來的資料

      $("#msg2").html("<font style='color:green;font-size:10px;' >code Confirmed</font>");

    }else{

       $("#msg2").html(alert("Sorry!Your code is not correct"));

       $("#code").val("");

    }

  }

  ,"text");

});

      $("#email").blur(function(){

        var a=$("#code").val().length;

        if(a<8){

          //   alert($(this).val());

        $.post("${pageContext.request.contextPath}/YZServlet.do",{email:$(this).val()},

        function(data){

          if(data=="true"){     //data 就是從servlet返回來的資料

            $("#msg1").html("<font style='color:green;font-size:10px;' >Email Confirmed</font>");

             /*$("#msg1").html(alert("Email Confirmedt"));*/

          }else{

            /* $("#msg1").html("<font style='color:red;font-size:10px;' >Email Error</font>");*/

             $("#msg1").html(alert("Sorry!Your email is not correct,please use kickstarter registered email!"));

             $("#email").val("");

          }

        }

        ,"text");

       a=$("#code").val().length;

}

      });

});

</script>