mvc2 下 jquery做普通验证

在做验证的时候,遇到了麻烦,本来想用jquery来做的,但是发现表单的存在,在提交后不管对错都不能进行拦截。后来找到了比较好的方法,在这里和大家分享下:

  
  
  
  
  1. <script type="text/javascript">  
  2.         $(document).ready(function () {  
  3.             $('input').each(function () {  
  4.                 $(this).focusout(function () {  
  5.                     if ($(this).val() == "")  
  6.                         $('#' + $(this).attr("type")).text("Required!")  
  7.                          .css('border-color''blue')  
  8.                         .css('border-style''groove');  
  9.                     else 
  10.                         $('#' + $(this).attr("type")).text("")  
  11.                         .css('border-style''none');  
  12.                 });  
  13.             });  
  14.             $('form').submit(function () {  
  15.                 var validate = true;  
  16.                 $('input').each(function () {  
  17.                     if ($(this).val() == "") {  
  18.                         validate = false;  
  19.                         $('#' + $(this).attr("type")).text("Required!")  
  20.                         .css('border-color''blue')  
  21.                         .css('border-style''groove');  
  22.                     }  
  23.                     else {  
  24.                         $('#' + $(this).attr("type")).text("")  
  25.                         .css('border-style''none');  
  26.                     }  
  27.                 });  
  28.                 return validate;  
  29.             });  
  30.         });  
  31.     </script>  

其中有个validate属性,可以来控制表单的提交。

你可能感兴趣的:(职场,jquery验证,休闲,validate属性)