简体   繁体   English

如何使用jquery验证动态表单字段?

[英]How to validate dynamic form fields using jquery?

I have a form with multiple input and drop down fields. 我有一个包含多个输入和下拉字段的表单。 I need confirmation before submitting. 我需要在提交前确认。 So, I have added dialogue box to confirm form submit by Submit or Cancel and I've prevented default submit. 所以,我添加了对话框,通过SubmitCancel确认表单提交,我已经阻止默认提交。

Now, When I use custom functions to validate input using jquery not working. 现在,当我使用自定义函数来验证使用jquery无法正常工作的输入时。

Even when I make input field like this: 即使我像这样输入字段:

<form class="form-horizontal" action="{{URL::to('quick/make/reservation')}}" method="post" role="form" id="confirm_request">

    <input type="text" name="contact" id="contact">
    <span id="error">
    </span>
    <a class="btn btn-default" data-toggle="modal" data-target="#confirm-verify" id="done">Done</a>
</form>
<div class="modal fade bd-example-modal-sm" id="confirm-verify" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
        <p class="text-left bold-text">Are you sure you want to Complete Reservation ?</p>
        </div>
        <div class="modal-body">
            <a type="button" class="btn btn-xs btn-danger btn-ok btn-sm" onclick="event.preventDefault();
                    document.getElementById('confirm_request').submit();" style="cursor: pointer;">Submit</a>
            <a type="button" class="btn btn-xs btn-primary btn-sm" data-dismiss="modal">Cancel</a>
        </div>
    </div>
    </div>
</div>

Empty data has been submitted. 已提交空数据。 I'm quite new to JQuery . 我是JQuery新手。

$('#done').click(function(event){
        var is_filled = true;
        var value = $('#contact').val();
        if(!value){
            is_filled = false;
        }

        if(!is_filled){
            event.preventDefault();
            $('#error').append('<strong>The field is required </strong>')
        }

    });

Anyone have idea, what I am doing wrong ? 任何人都有想法,我做错了什么?

Here is the example of your code where I've changed some code. 以下是我更改了一些代码的代码示例。 I hope it will work. 我希望它能奏效。 I have done below changes on your code. 我对您的代码进行了以下更改。

  1. Removed data-toggle="modal" data-target="#confirm-verify" from your Done button, because popup opens even if code is validated. Done按钮中删除了data-toggle="modal" data-target="#confirm-verify" ,因为即使验证了代码,弹出窗口也会打开。
  2. Removed event.preventDefault(); 删除了event.preventDefault(); from Submit button of Popup. 来自Popup的Submit按钮。
  3. Added code to open popup $("#confirm-verify").modal("show"); 添加了打开弹出窗口的代码$("#confirm-verify").modal("show"); in the else condition. 在其他条件下。

Now if you have not entered value in textbox it will be validated and popup will not open. 现在,如果您没有在文本框中输入值,它将被验证,弹出窗口将无法打开。 When you enter something on textbox end click on 'Done` button the popup will open because you have entered value in it. 当您在文本框上输入内容时,单击“完成”按钮,弹出窗口将打开,因为您已在其中输入值。

After Clicking on Submit button of popup, will submit to the particular url. 单击弹出的“ Submit按钮后,将提交给特定的URL。

 $('#done').click(function(event){ var is_filled = true; var value = $('#contact').val(); if(!value){ is_filled = false; } if(!is_filled){ event.preventDefault(); $('#error').html('<strong>The field is required </strong>') } else { $("#confirm-verify").modal("show"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <form class="form-horizontal" action="get-data" method="post" role="form" id="confirm_request"> {{csrf_field()}} <input type="text" name="contact" id="contact"> <span id="error"> </span> <a class="btn btn-default" href="javascript:;" id="done">Done</a> </form> <div class="modal fade bd-example-modal-sm" id="confirm-verify" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <p class="text-left bold-text">Are you sure you want to Complete Reservation ?</p> </div> <div class="modal-body"> <a type="button" class="btn btn-xs btn-danger btn-ok btn-sm" onclick="document.getElementById('confirm_request').submit();" style="cursor: pointer;">Submit</a> <a type="button" class="btn btn-xs btn-primary btn-sm" data-dismiss="modal">Cancel</a> </div> </div> </div> </div> 

I would suggest to make disable submit button with $("#submit").attr("disabled",true) if there is an error about your input. 如果输入错误$("#submit").attr("disabled",true)我建议使用$("#submit").attr("disabled",true)来禁用提交按钮。 If not make enable with $("#submit").attr("disabled",false) 如果没有使用$("#submit").attr("disabled",false)启用$("#submit").attr("disabled",false)

You can append error if your input is null and if not you can remove your error text like following snippet. 如果输入为空,则可以附加错误,如果不是,则可以删除错误文本,如下面的代码段。

 $('#done').click(function(event){ var value = $('#contact').val(); if(!value){ $("#submit").attr("disabled",true); $('#error').append('<strong style="color:red">The field is required </strong>') } else { $("#submit").attr("disabled",false); $("#error strong").remove(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <form class="form-horizontal" method="post" role="form" id="confirm_request"> <input type="text" name="contact" id="contact"> <span id="error"> </span> <a class="btn btn-default" data-toggle="modal" data-target="#confirm-verify" id="done">Done</a> </form> <div class="modal fade bd-example-modal-sm" id="confirm-verify" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <p class="text-left bold-text">Are you sure you want to Complete Reservation ?</p> </div> <div class="modal-body"> <input value="Submit" type="button" id="submit" class="btn btn-xs btn-danger btn-ok btn-sm" onclick="" style="cursor: pointer;"></input> <input value="Cancel" type="button" class="btn btn-xs btn-primary btn-sm" data-dismiss="modal"></input> </div> </div> </div> </div> 

您可以使用html5表单验证,在您的情况下我会使用'required'属性,然后使用submit事件而不是click并执行preventDefault,然后使用form.checkValidity函数查看它是否有效,在此测试后您可以添加自己的验证并通过ajax而不是form submition发送数据

Using JQuery validate plugin you can achieve this kind of validations with ease. 使用JQuery validate插件,您可以轻松实现这种验证。 Also it provides lot of default validations: 它还提供了许多默认验证:

As an example: HTML 举个例子: HTML

<form id="form" method="post" action="#">
<label for="name">Name</label>
<input type="text" name="name" id="name" /><br/>
<label for="email">Email</label>
<input type="email" name="email" id="email" />
<button type="submit">Submit</button>

Validations using JQuery 使用JQuery进行验证

$(document).ready(function () {
$("#form").validate({
    rules: {
        "name": {
            required: true,
            minlength: 5
        },
        "email": {
            required: true,
            email: true
        }
    },
    messages: {
        "name": {
            required: "Please, enter a name"
        },
        "email": {
            required: "Please, enter an email",
            email: "Email is invalid"
        }
    },
    submitHandler: function (form) { // for demo
        alert('valid form submitted'); // for demo
        return false; // for demo
    }
});});

Here is JSFiddle demo . 这是JSFiddle演示 The best part is the validation happens on the fly. 最好的部分是即时验证。 You don't have to submit the form. 您无需提交表单。

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

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