jquery.validate.js 表单验证实例精选

2010-10-27 14:32:53 by 【6yang】, 514 visits, 收藏 | 返回

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" lang="utf-8"><head>

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>注册</title>
 <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>

<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
 
   #field, label {font-family: Arial, Helvetica, sans-serif; font-size: small; }

 input.submit { float: none; }

 input.error,select.error { border: 1px solid red; width: auto; }
 label.error {
  background: url('./reg_files/unchecked.gif') no-repeat;
  padding-left: 16px;
  margin-left: .3em;
 }
 label.valid {
  background: url('./reg_files/checked.gif') no-repeat;
  width: 16px;
  height: 16px;
 }
</style>

<script type="text/javascript">
 function regist(callback){
   var aaa = this;
  
   this.init = function(){
       aaa.addnotice();
       aaa.addm();
       aaa.check();
       aaa.unchange();
       return aaa;
   }
  
   this.addnotice = function(){

       $("input[type='text']").each(function(){
         $(this)
         .focus(function(){
          if($(this).val() == $(this).attr('notice')){
           $(this).val("");
          }
         })
         .blur(function(){
          if($(this).val() == $(this).attr('notice') || $.trim($(this).val()) == ""){
           $(this).val($(this).attr('notice'));
          }
         });
       });  
   }

   this.cleannotice = function(){
       $("input[type='text']").each(function(){
         if($(this).val() == $(this).attr('notice')){
          $(this).val("");
         }
       });     
   }

   this.unchange = function(){
       $(".select").bind('change',function(){
         if($(this).val() == '0'){
          $("#citySelect").attr("class","error").html('请选择区域').show();
          $("#RegionId").attr("class","error");
         }else{
          $("#citySelect").attr("class","valid").html('success').show();
          $("#RegionId").attr("class","valid");
         }
       });     
   }
        
   this.addm = function(){
   jQuery.validator.addMethod("iszipcode", function(value, element) {   
     var tel = /^[0-9]{6}$/;   
     return this.optional(element) || (tel.test(value));   
   }, "请正确填写您的邮政编码");   
  
   jQuery.validator.addMethod("engname", function(value, element) {   
     var tel = /^[a-zA-Z]*$/;   
     return this.optional(element) || (tel.test(value));   
   }, "请输入英文字符");     
   }
  
   this.check = function(){
    $('#regist_form').submit(function(){
  aaa.cleannotice();
  
 });
  
 $("#regist_form").validate({
    rules: {
      Name: "required",
      engName:{
       required:true,
       engname:true,
       rangelength:[4,10]
   },
      Email: {
        required:true,
        email:true,
        rangelength:[4,50]
      },
      postCode: {
        required: true,
        rangelength:[6,6],
        iszipcode: true
      },
      phone: {
        required:true,
        number:true,
        rangelength:[11,11]
      }
    },
    messages: {
      Name: "请输入名字",
      engName: {
        required: "请输入英文名",
        rangelength: jQuery.format("长度请控制在{0} ~ {1}")
      },
      Email: {
        required: "请输入EMAIL",
        email: "请输入正确的EMAIL"
      },
      postCode: {
        required: "请输入邮编",
        rangelength:jQuery.format("长度必须是6位")
      },
      phone: {
        required: "请输入手机号",
        number: "请输入数字",
        rangelength: jQuery.format("长度必须是11位")
      }
    },
    success: function(label) {
      label.addClass("valid").text("succuess!");
     
    },
    submitHandler: function(form) {
   if($("#RegionId").val() == '0'){   
    $("#citySelect").attr("class","error").html('请选择区域').show();
    $("#RegionId").attr("class","error");
   }else{
    $("#RegionId").attr("class","valid");
    $("#citySelect").attr("class","valid").html('success').show();
    form.submit();
   }
    }
 });
   }

}
</script>
 </head>
 <body>
 <br><br>
 

 <br><br>
  <form id="regist_form" method='post'>
 
        <div class="td leftCol">

           名字: <input name="Name" id="Name" class="inputVal" tabindex="6005" type="text" value="请输入中文名字" notice="请输入中文名字" >
          <label class="error reminder" generated="true" for="Name" style="display: none;"></label>
        </div>
        <div class="clear"></div> 

        <div class="td leftCol">
            英文名字:<input name="engName" id="engName" class="inputVal" tabindex="6005" type="text" value="请输入英文名字" notice="请输入英文名字">
          <label class="error reminder" generated="true" for="engName" style="display: none;"></label>
        </div>

        <div class="clear"></div> 

        <div class="td leftCol">
              区域选择:  <select name="RegionId" id="RegionId" class="select" tabindex="6006">
                    <option value="0" selected="selected">行政区选择</option>
             <option value="12">浦东新区</option>
             <option value="42">松江区</option>
             <option value="41">金山区</option>

             <option value="40">崇明区</option>
             <option value="39">青浦区</option>
             <option value="37">静安区</option>
             <option value="36">徐汇区</option>
             <option value="35">长宁区</option>
             <option value="34">虹口区</option>

             <option value="33">闸北区</option>
             <option value="32">宝山区</option>
             <option value="31">嘉定区</option>
             <option value="30">闵行区</option>
             <option value="29">普陀区</option>
             <option value="28">卢湾区</option>

             <option value="27">黄浦区</option>
             <option value="26">杨浦区</option>
             <option value="43">奉贤区</option>
             </select>

          <label class="error reminder" id="citySelect"  for="citySelect" style="display: none;padding-left: 15px;"></label>
        </div>
        <div class="clear"></div> 
 
         <div class="td leftCol">

               邮政编码: <input name="postCode" id="postCode" class="inputVal" tabindex="6005" type="text"  value="请输入邮编" notice="请输入邮编">
          <label class="error reminder" generated="true" for="postCode" style="display: none;"></label>
        </div>
        <div class="clear"></div> 

         <div class="td leftCol">
              Email: <input name="Email" id="Email" class="inputVal" tabindex="6005" type="text" value="请输入Email" notice="请输入Email">

          <label class="error reminder" generated="true" for="Email" style="display: none;"></label>
        </div>

        <div class="clear"></div> 

         <div class="td leftCol">
               电话: <input name="phone" id="phone" class="inputVal" tabindex="6005" type="text" value="请输入电话" notice="请输入电话">
          <label class="error reminder" generated="true" for="phone" style="display: none;"></label>
        </div>
        <div class="clear"></div> 
        <button name="submit" type="submit">下一步</button>
       </form>    
                             
 </body>

 <html>
 <script type="text/javascript">
  $(document).ready(function(){
   new regist().init();
  });
 </script>

来源

http://blog.51yip.com/demo/regist.htm

 

分享到:
share

    图片原图

    loading

    loading