简体   繁体   English

javascript - 表单验证 - 禁用按钮

[英]javascript - form validation - disabled button

I'm new to programming so please forgive me if this is a dumb question...我是编程新手,所以如果这是一个愚蠢的问题,请原谅我...

I'm attempting to have javascript (or jquery - not sure of the difference) toggle the 'disabled' property of the submit button based on periodic form validation checks.我正在尝试让 javascript(或 jquery - 不确定区别)根据定期表单验证检查切换提交按钮的“禁用”属性。 It works fine when I key everything into the form, but if I copy and paste from the first password field into the second password field, the disabled property isn't removed from the button.当我将所有内容都输入表单时它工作正常,但是如果我从第一个密码字段复制并粘贴到第二个密码字段,则不会从按钮中删除禁用属性。

Any help would be very much appreciated...任何帮助将不胜感激...

Javascript... Javascript...

      $(':input').focusin(function(){

       $(this).css('background-color','#AABEFF');

        }).blur(function(){

       $(this).css('background-color','white');
          });

      function check(){

      if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
      ($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")){

           if (($('#password1').val())==($('#password2').val()))
              {
              $('#submit').removeAttr('disabled');
              }

           else

            {
             $('#submit').attr('disabled','disabled');
            }
      }

        else

            {
             $('#submit').attr('disabled','disabled');
            }
      }

      window.setInterval(check, 200);

Form....形式....

        <html>
        <head>

          <link rel="stylesheet" type="text/css" href="style.css" />
        </head>

              <body>
                 <div id="back">
                 </div>

                <div id="wrapper">

                        <div id="header">
                         <div id="logout">
                        <?php
                      include 'employer_header.php';
                        ?>

                         </div>

                        </div>

                        <?php
                        include 'employer_menu.php';
                        ?>

                        <div id="feature">
                        </div>
                        <div id="content">

                        <div class="pad">

        <form method="post" action="employer_register_process.php">

        <p>First Name: <br>
        <input id="first" type="text" name="firstname" size="30"><br></p>
        <p>Last Name: <br>
        <input id="last" type="text" name="lastname" size="30"><br></p>
        <p>Phone 1: <br>
        <input id="phone1a" type="text" name="phone1a" style='width:30px' maxlength="3">-
        <input id="phone1b" type="text" name="phone1b" style='width:30px' maxlength="3">-
        <input id="phone1c" type="text" name="phone1c" style='width:40px' maxlength="4"><br></p>
        <p>Phone 2 (Optional): <br>
        <input type="text" name="phone2a" style='width:30px' maxlength="3">-
        <input type="text" name="phone2b" style='width:30px' maxlength="3">-
        <input type="text" name="phone2c" style='width:40px' maxlength="4"><br></p>
        <p>email: <br>
        <input id="email" type="text" name="email" size="30"><br></p>
        <p>password: <br>
        <input id="password1" type="password" name="pass1" size="30" mask="x"><br></p>
        <p>retype password: <br>
        <input id="password2" type="password" name="pass2" size="30" mask="x"><br></p>


        <p ><input id='submit' type="submit" name='submit' value='register' disabled='disabled'></p>
        </form>

                      </div>
                      </div>
                      </div>
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/register.js"></script>
              </body>
        </html>

You can add a onKeyUp listener to a text field, and make disable, or if the value is correct, enable, the button's disabled property.您可以将 onKeyUp 侦听器添加到文本字段,并禁用按钮的禁用属性,或者如果值正确,则启用该属性。

HTML: HTML:

    <input type="text" onkeyup="validate(this)" />

JavaScript: JavaScript:

   function validate(textfield) {

   var submitButton = document.getElementById("submit_button");
   submitButton.disabled = true;
   if(textfield.value == "good") {
          submitButton.disabled = false;
   }
   }

Something like that should be good.这样的东西应该不错。

Oh, and JQuery is a framework built on top of Javascript.哦,JQuery 是一个建立在 Javascript 之上的框架。

you are not calling your check function.你不是在调用你的检查函数。
you should use jquery .keyup() function to check你应该使用 jquery .keyup() 函数来检查
Here is an example of what your file would look like:以下是您的文件的外观示例:

$(':input').focusin(function(){

    $(this).css('background-color','#AABEFF');

}).blur(function(){

    $(this).css('background-color','white');

});

function checkForm(){

  if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
  ($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")){

       if (($('#password1').val())==($('#password2').val()))
          {
          $('#submit').removeAttr('disabled');
          }

       else

        {
         $('#submit').attr('disabled','disabled');
        }
  }

    else

        {
         $('#submit').attr('disabled','disabled');
        }
  }

  $('#password2').keyup(function(){
         checkForm();
  });

hope that helps希望有帮助

I'm not entirely sure what is wrong with your code but this definitely works and is a better way of doing it:我不完全确定您的代码有什么问题,但这绝对有效并且是一种更好的方法:

$(':input').focusin(function(){
    $(this).css('background-color','#AABEFF');
}).blur(function(){
    $(this).css('background-color','white');
});

$('input').keyup(function(){

    if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
  ($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")) {

        if ( $('#password1').val() == $('#password2').val() ) {
            $('#submit').removeAttr('disabled');
        } else {
            $('#submit').attr('disabled','disabled');
        }

    } else {
       $('#submit').attr('disabled','disabled');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM