Javascript - Disable submit button until 3 fields are not empty

随声附和 提交于 2019-12-11 12:04:55

问题


I have this almost working bit: http://jsfiddle.net/HFzkW/ My code looks like this:

<div id="cform-container">
<div class="alignleft" id="cform">
    <form action="<?php print site_url('/sent'); ?>" method="POST" >
    <input type="hidden" name="Contact-Form" value="contact-form" />
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="yn">Your Name</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="yn" id="yn" /><br /><span id="namespan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="cn">Company Name</label></div>
            <div class="alignright cform-ti"><input type="text" name="cn" id="cn" /></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="em">Email Address</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="em" id="em" /><br /><span id="emailspan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="ph">Phone Number</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="ph" id="ph" /><br /><span id="phonespan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="ct">Industry</label></div>
            <div class="alignright cform-ti"><input type="text" name="ct" id="ct" /></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="msg">Message</label></div>
            <div class="alignright cform-ti"><textarea name="msg" id="msg"></textarea></div>
            <div class="clearboth"></div>
        </div>
        <div id="cform-btn"><input type="submit" value="Submit" /></div>
    </form>
</div>
<script id="source" language="javascript" type="text/javascript">
 $(function(){  
    $("input[type=submit]").attr("disabled", "disabled");
    $("#em").blur(function() 
            {
             var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
             var emailaddress = $("#em").val();
             if(!emailReg.test(emailaddress) | emailaddress=="") {
                $("#emailspan").html('<font color="#cc0000">Please enter valid Email address</font>');
             }else{
                $("#emailspan").html('<font color="#cc0000"></font>');  
                }
            });
    $("#yn").blur(function() 
            {
             var namefield = $("#yn").val();
             if(namefield=="") {
                $("#namespan").html('<font color="#cc0000">Please enter your name</font>');  
             }else{
                $("#namespan").html('<font color="#cc0000"></font>');  
                }
            });                                 
    $("#ph").blur(function() 
            {
             var phonefield = $("#ph").val();
             if(phonefield=="") {
                $("#phonespan").html('<font color="#cc0000">Please enter your name</font>');  
             }else{
                $("#phonespan").html('<font color="#cc0000"></font>');  
                }
            });
$('input, textarea').change(function(){
    var validation;
    var email = $("#em").val();
    var name = $("#yn").val();
    var phone = $("#ph").val();

    if ( email == "" && name == "" && phone == "" ) {
    validation = false;
    } else if ( email != "" && name != "" && phone != "" ) {
    alert ("variables"+email+name+phone);
    validation = true;
    }
    if (validation = true) {
    $("input[type=submit]").removeAttr("disabled"); 
    } else {
    $("input[type=submit]").attr("disabled", "disabled");
    }
});
});

I cannot get it to work completely however. When the user enters one of the fields to (such as Your Name) the Submit button is no longer disabled.

I want to keep the Submit button disabled until all 3 of the 'required' fields are met. I thought the code was pretty clear for that - but it still isn't working.

Also, it works here in JSFiddle, but isn't working on the Wordpress site where I'm trying to get it working. Any ideas for checking that would also be appreciated.


回答1:


Try switching ands to ors. Be careful with your logic and try to say it out loud to see if it makes sense:

Email is blank and name is blank and phone is blank then validation is false. What you actually wanted was to say if any field is blank, validation is false.

if ( email == "" || name == "" || phone == "" ) {
validation = false;
} else if ( email != "" && name != "" && phone != "" ) {
alert ("variables"+email+name+phone);
validation = true;
}

Also, you are setting validation=true in your if statement. This will always return true. Change to this: if (validation == true) {




回答2:


From what I can see, the first things I would do to debug this (The code is a little confusing so rather then copy it i will give you some guidelines).

instead of

 if(validation = true )

try

 if(validation)

This is because without any parameters it will check for not being false, undefined or null. The same can probably be done for email name and phone.




回答3:


So basically:

<form id="myForm" onfocus="submitButton("myForm);">
  <input type="text" name="name" placeholder="Name">
  <input type="text" name="email" placeholder="Email">
  <input type="text" name="phone" placeholder="Phone">
</form>

And

function submitButton(formID){

  var formObject = document.getElementById(formID);

  var name = formObject.name.value;
  var email = formObject.email.value;
  var phone = formObject.phone.value;

  if (name && email && phone) {
    showSubmitButton();
  } else {
    //keep the button hidden
  }
}


来源:https://stackoverflow.com/questions/16157763/javascript-disable-submit-button-until-3-fields-are-not-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!