简体   繁体   English

必填字段上的Javascript / Jquery验证

[英]Javascript / Jquery Validation on required fields

I searched a lot but couldn't find an answer for my question. 我进行了很多搜索,但找不到我的问题的答案。 Firstly, i would like to have a submit button which will be disable if all the required fields are not filled. 首先,我想有一个提交按钮,如果没有填写所有必填字段,该按钮将被禁用。 I found some example if all the fields are filled but i have required and not required ones So, I need to find if the required ones are filled or not. 我找到了一些示例,说明所有字段是否都已填写,但是我已经填写了必填字段,而不是必填字段。 I am currently out of ideas of how to make it happen. 我目前对如何实现它没有任何想法。 Just in case , i am using play framework 2.1.x with Scala. 以防万一,我在Scala中使用play框架2.1.x。

Cheers, Ilgün 伊尔干,干杯

Here is a simple example: 这是一个简单的示例:

First you can identify the required fields with a class of required: 首先,您可以使用必填类来标识必填字段:

<form id="myForm">
    *<input type="text" class="required" /></br>
    *<input type="text" class="required" /></br>
    <input type="text" /></br>
    <input type="text" /></br>
    <input type="submit"/>
</form>

Initially disable your submit button: 最初禁用您的提交按钮:

var submitButton = $("#myForm input[type='submit']").attr("disabled", true);

Then use jQuery to loop through your inputs everytime one changes, and check if there are values in them. 然后,使用jQuery每次更改输入时都会遍历您的输入,并检查其中是否有值。 If they all have a value then enable the submit button, if not disable it: 如果它们都有一个值,则启用提交按钮,如果不禁用它,则:

    $("#myForm input.required").change(function () {
        var valid = true;
        $.each($("#myForm input.required"), function (index, value) {
            if(!$(value).val()){
               valid = false;
            }
        });
        if(valid){
            $(submitButton).attr("disabled", false);
        } 
        else{
            $(submitButton).attr("disabled", true);
        }
    });

Please see this jsFiddle with a simple example of what you are describing using jQuery. 请参阅此jsFiddle以及您使用jQuery描述的简单示例。

This is just some basic stuff with plain HTML/JS to show a possible way of input validation: 这只是纯HTML / JS的一些基本内容,用于显示输入验证的可能方法:

HTML: HTML:

    <html>
      <form id="form">
        <input class="form-input" data-required="true" type="text" />
        <input class="form-input" data-required="true" type="text" />

        <input class="form-input" data-required="false" type="text" /> 
        <input class="form-input" data-required="false" type="text" />

        <input id="form-submit" type="submit" disabled />
      </form>
    </html>

JS: JS:

    $(document).ready(function() {
      // on input change:
      $(".form-input").on('change', function() {
        // hide the input (not neccessary)
        $("#form-submit").hide();
        // enable it
        $("#form-submit").removeProp("disabled");

        // check all inputs
        $.each(".form-input", function() {
          // read element values
          var required = $(this).data("required");
          var empty = ($(this).val() == "" || $(this).val() == null)
          // if element is required but empty:
          if (required && empty) {
            // disable submit button
            $("#form-submit").prop("disabled", "disabled");
          }
        }          

        // now the submit button is enabled if all required inputs are filled
        // and it is disabled if at least one required input is empty

        // after hiding it: make the button visible again (not neccessary)
        $("#form-submit").show();
      }
    });

I have been trying to use your solution, 我一直在尝试使用您的解决方案,

this is my input : 这是我的输入:

<input class="required" placeholder="Required Field" required="true" name="tracktitle" type="text">

However the button is not turning to enabled state. 但是,按钮没有变成启用状态。

JS: JS:

<script>
$(function () {
    var submitButton = $("#myForm input[type='submit']").attr("disabled", true);
    $("#myForm input.required").change(function () {
        var valid = true;
        $.each($("#myForm input.required"), function (index, value) {
            alert("comes here");
            if(!$(value).val()){
               valid = false;
            }
        });
        if(valid){
            $(submitButton).attr("disabled", false);
            console.log("valid");
            alert("valid");
        } 
        else{
             $(submitButton).attr("disabled", true);
        }
    });
});

</script>

My submit Button: 我的提交按钮:

<input type="submit" class="btn btn-primary" value="Submit" id="form-submit" onclick="return myConfirm();" />

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

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