简体   繁体   中英

activate submit button only when all the fields are filled in step wizard form

I have a horizontal one page website with multi step forms included in each page with a next button following to redirect to the second page.

I want such that a user can't proceed to the next page until he/she fills all the required fields in the given forms.

Here is the abridged code which have only limited choices for month, year and nationality:

    <div class="step-content hide">
    <div class="row">
        <div class="col-sm-12">
            <div class="col-sm-4" id="artle_tellusmore_label_para" style="float:left;">
                <label id="artle_tellusmore_label">Date of Birth</label id="artle_tellusmore_label">
                <label id="artle_tellusmore_label">Gender</label>

                <hr>
                <div style="margin-left: 38px;" id="artle_tellusmore_profileimageupload">
                    <img style="height: 180px; width:165px;" id="output" src="image/artle_dp_dummy.png">

                </div>
            </div>
            <div style="margin-left: 248px;" class="col-sm-8">
                <div id="artle_date_of_birth_combo_box">
                    <select style="width: 60px;" name="birthday">
                        <option value="">-Day-</option>
                        <option value="">1</option>
                        <option value="">2</option>

                    </select>

                    <select style="width: 91px;" name="birthmonth">
                        <option value="">-Month-</option>
                        <option value="">January</option>
                        <option value="">February</option> 
                    </select>

                    <select style="width: 67px;" name="birthyear">
                        <option value="">-Year-</option>
                        <option value="2007">2007</option>
                        <option value="2006">2006</option>

                    </select>
                </div>
                <div id="artle_tellusmore_gender_radio_button">
                    <input class="radio" type="radio"><span id="">Male</span>&nbsp&nbsp&nbsp&nbsp&nbsp

                    <input class="radio" type="radio"><span id="">Female</span>
                </div>

                <div id="artle_tellusmore_phone_number">

                    <input style="width: 223px; margin-top: 10px; height: 33px;" required="" pattern="[0-9]{10}" x-moz-errormessage="Enter a valid phone number" size="10" value="" placeholder="Phone Number" type="tel">
                </div>

                <div id="artle_tellusmore_nationality">
                    <select name="nationality" style="width: 225px;">
                        <option value="">-- Nationality --</option>
                        <option value="afghan">Afghan</option>
                        <option value="albanian">Albanian</option>

                        <option value="zimbabwean">Zimbabwean</option>
                    </select>
                </div>
                <div id="artle_tellusmore_state">
                    <input type="text" value="" placeholder="State" style="width: 223px; height: 33px;" />

                </div>
                <hr>
                <div id="artle_tellusmore_">
                    <input type="file" name="file" accept="image/*" onchange="loadFile(event)" id="artistprofileuploadfileField">

                    <p style="margin-top: 15px; font-size: 1.1em;">You can upload a jpg, gif or png files</p>
                </div>

            </div>

        </div>
        <div style="clear:both;"></div>
        <button class="next-button btn hide" id="artle_next_button">Next</button>
        <input type="submit" class="submit-button btn" id="artle_next_button" value="Done" />
        <button class="back-button btn" id="artle_next_button">Back</button>
    </div>

</div>

By looking at your html I don't think you are using HTML5. I assume you want all form fields with class form-field to be validated.

In that case you can use this code:

first check that all form elements have values:

function validateForm(form) {
  var isValid = true;
  $('.form-field').each(function() {
    if ( $(this).val() === '' )
        isValid = false;
  });
  return isValid;
}

Then use this change event to track if anything is changed at form elements then validate the form and enable or disable next button

 $(".form-field").change(function() {
      if (validateForm() )
        $(".next-button").show() 
      else 
        $(".next-button").hide() 
    });

Please take above code as a direction.

Can't give you an exact answer due to lack of code you have tried. but you are looking for something like this.

$(document).ready(function(){
    $('form').find('select').change(function() {
        var disabled = false;
        $('form').find('select').each(function() {
            if ($(this).find('option:selected').val() === '') {
                disabled = true;
            }
        });
        $('form').find('input[type="submit"]').prop('disabled', disabled);
    });
});

for validate all inputs, you can do something like this.

$("form").submit(function(event) {
    var isValid = true;
    $('input[type="text"]').each(function() {
        if ( $(this).val() === '' )
            isValid = false;            
    });
    if (!isValid) event.preventDefault();
});

for activate submit button against all inputs.

$('input[type="text"]').on('input', function() {
    var isValid = true;
    $('input[type="text"]').each(function() {
        if ( $(this).val() === '' )
            isValid = false;            
    });
    if (!isValid) $('input[type="submit"]').attr('disabled','disabled');
    else $('input[type="submit"]').removeAttr('disabled');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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