2012-05-06 81 views
0

我已经成功地管理,以实现实时的表单验证我的注册页面,这一切的伟大工程,除了我要禁用的形式向上提交按钮,直到点所有的输入是正确的,用户已经检查了两个复选框(条款和条件以及隐私政策)。我曾尝试给它一个去,但我从来没有做过,所以不知道从哪里开始,我问过几个人,显然这是与在下面的JavaScript底部返回false。任何帮助将非常感激。由于禁用提交按钮,直到所有输入正确

HTML表单

<div id="overlay"> 


<div id="modal_wrapper"> 
     <div id="modal_content"> 

     <form id="jform" action="" method="post" enctype="multipart/form-data"> 
      <div id="modal_content_left"> 
      Register 

      <ul> 

       <li class="required" type="none"> 

        <label> 

         <input type="text" class="reg_text tip" name="username" id="username" title="8 - 16 Characters" placeholder="Username*"/> 

        </label> 

        <label> 
         <br /> 
         <input type="text" class="reg_text tip" name="email" id="email" title="Get Verified" placeholder="Email*"/> 

        </label> 
        <label> 
         <br /> 
         <input type="password" class="reg_text tip" name="password" id="password" title="Min 8 Characters" placeholder="Password*"/> 

        </label> 


        <label> 
         <br /> 
         <input type="password" class="reg_text tip" name="cpassword" id="cpassword" title="Min 8 Characters" placeholder="Verify Password*"/> 
        </label> 
       </li> 


      </ul> 

      </div><!---end modal_content_left---> 

      <div id="modal_content_right"> 

      <!--<li class="required double" type="none">--> 


       <div id="payment_options"> 

        <p> 
         <label><img src="images/index/images/paypal.png" width="78" height="38" alt="PayPal" /> 
          <br /> 
          <input type="radio" name="payment_selection" value="paypal" id="payment_selection_0" checked/> 
          </label> 

         <label><img src="images/index/images/alertpay.png" width="78" height="38" alt="Alertpay" /> 
          <br /> 
          <input type="radio" name="payment_selection" value="alertpay" id="payment_selection_1" /> 
          </label> 

          <label><img src="images/index/images/check.png" width="78" height="38" alt="Check" /><br /> 
          <input type="radio" name="payment_selection" value="check" id="payment_selection_2" /> 
          </label> 

      </p> 
      <br /><br /><br /><br /> 
        Account Type 

        <p> 
         <label><img src="images/index/images/demo.png" width="78" height="38" alt="Demo" /> 
         <br /> 
         <input type="radio" name="account_type" value="demo" class="demo_radio" id="account_type_0" /> 
         </label> 

         <label><img src="images/index/images/real_account.png" width="78" height="38" alt="Real" /><br /> 
         <input type="radio" name="account_type" value="real" id="account_type_1" checked/> 
         </label> 

        </p> 
       </div><!---end payment_options---><br /><br /><br /> 

       <div id="final_reg"> 
       <div id="check_boxes"> 
       <p> 
       <li class="required" type="none"> 
       <label> 
        <input type="checkbox" name="agree_cons" value="terms" id="agree_cons_0" /> 
        I agree to Terms</label> 
       <br /><br /> 
       <label> 
        <input type="checkbox" name="agree_cons" value="privacy" id="agree_cons_1" /> 
        I agree to Privacy Policy</label> 
        </li> 

       <br /> 
       </p></div><!---end check_boxes---> 
       <div id="register_submit"> 
       <input name="submit" type="submit" class="register_submit" 
         id="register_submit" alt="Register" /> 
       </div><!---end register_submit---> 
      </div><!---end final_reg---> 
     </div><!---end mofal_content_right---> 

     </form> 
     </div><!---end modal_content---> 
     <div id="error_display"></div><!---end error_display---> 
</div><!---end modal_wrapper---> 

JavaScript代码

$(document).ready(function() { 
jFunc={ 
    mustMatch: function(el,pt){ 
      if (pt.test(el.val())) 
       el.removeClass('error').addClass('correct') 
      else{ 
       jVal.errors = true; 
       el.removeClass('correct').addClass('error').effect("shake", { times:3, distance:5 }, 60); 
      } 
     }, 
    mustEqual: function (el,val){ 
      if (el.val()==val) 
       el.removeClass('error').addClass('correct') 
      else{ 
       jVal.errors = true; 
       el.removeClass('correct').addClass('error').effect("shake", { times:3, distance:5 }, 60); 
      } 
     } 
    } 
jVal = { 
    'username': function() { 
     jFunc.mustMatch($('#username'),/^(?=.*[a-z].*)\w{8,}$/i); 
    }, 
    'email': function() { 
     jFunc.mustMatch($('#email'),/^[email protected]+[.].{2,}$/i); 
    }, 
    'password': function() { 
     jFunc.mustMatch($('#password'),/^.{8,}$/i); 
    }, 
    'cpassword': function() { 
     jFunc.mustEqual($('#cpassword'),$('#password').val()); 
    }, 
}; 
$('#username').change(jVal.username); 
$('#email').change(jVal.email); 
$('#password').change(jVal.password); 
$('#cpassword').change(jVal.cpassword); 
$('#jform').submit(function(){ 
    $.each(jVal,function(i,v){ 
     $.isFunction(v) && v() 
    }) 
    return false // fix this when the code works. 
}) 

});

+3

,如果你在同一个片段让这些代码片段那就只有更好,它被划分为不明原因的那些 –

+0

你好,对不起,我不知道为什么这一切分手了,我现在已经修改它,谢谢 – Arken

+0

这一切都打破了随机outdents的原因。 :P但是,真的,这个问题是否真的需要粘贴HTML的每一点?如果你能把HTML缩小到一个最小但可行的例子,这将是有帮助的。 – cHao

回答

1

除了现有的事件处理程序,你可以不喜欢

$('#username,#email,#password,#cpassword').change(function() { 
    jVal.errors = false; 
    $.each(jVal, function(i, validator) { 
     if ($.isFunction(validator)) { validator(); } 
    }); 
    $('#register_submit').prop('disabled', jVal.errors); 
}); 

但是请注意,你可能希望你的提交处理检查提交按钮是否被禁用(和return false如果是,所以表单不提交),而不是自己再次进行验证。这样所有的验证都发生在同一个地方。

(当然,只是所以它说,没有这避免了在服务器端的数据的适当的验证需要。)

+0

好吧,但我可以添加什么来启用/禁用提交按钮。此外,我也在做服务器端验证,我只是想给用户提供基本验证,以实时停止不断运行php – Arken

+0

的需求。我已经添加了一个示例。 – cHao

+0

伟大的代码看起来不错,但它说,有六号线的误差($(“#register_submit”)丙(“禁用”,jVal.errors);)我认为这是没有验证的原因一直工作,我看了一下,但找不到错误可能是什么 – Arken

相关问题