0

I am getting the below image error when trying to simply validate a form. The error to me sounds like the validate plugin (jquery.validate.min.js) can't be found in the cdn version of jQuery we are using.

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js"></script>

Here is the validation code I am using in the JS:

$('#btnSPAcceptShippingReq').on('click', function (e)
    {
        $("#form1").validate({
            rules: {
                txtSPStatusComments: {
                    required: true
                }
            },
            messages: {
                txtSPStatusComments: {
                    required: "Status is a required field!"
                }
            },
            highlight: function (e)
            {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },

            success: function (e)
            {
                $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
                $(e).remove();
            },

            errorPlacement: function (error, element)
            {
                error.insertAfter(element.parent());
            }
        });

How can I get this simple plugin included in my project?

enter image description here

EDIT

jQuery JS validation

$("#form1").validate({
        rules: {
            txtSPStatusComments: {
                required: true
            }
        },
        messages: {
            txtSPStatusComments: {
               required: "Status is a required field!"
            }
        },
        highlight: function (e)
        {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },

        success: function (e)
        {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            $(e).remove();
        },

        errorPlacement: function (error, element)
        {
            error.insertAfter(element.parent());
        }
    });

EDIT #2

As I mentioned before, I don't think the jQuery validate plugin works with bootstrap because it seems that the jQuery plugin requires "input type="submit" whereas bootstrap requires the following ("button type="submit"). If I change the "button type" to "input type" in the HTML, bootstrap doesn't accept this.

<button type="submit" id="btnSPAcceptShippingReq" class="btn btn-primary">Accept</button>

However, if I change the HTML in the jsFiddle to the below, it still works. So, I don't know what I'm missing in regards to the validation not firing when I click on the submit button....

<button type="submit" />

Note that while debugging, I do see the JS Validator code getting hit and initialized.

Note that I can't get the bootstrapValidator to work either as explained in this link: Bootstrap Validator submits form even if there is a validation error

If someone can help me out with that, I would greatly appreciate it.

Here's the code:

HTML MasterPage

    <!-- page specific plugin styles -->
    <link rel="stylesheet" href="../Content/assets/css/jquery-ui.min.css" />
    <link rel="stylesheet" href="../Content/assets/css/datepicker.css" />
    <link rel="stylesheet" href="../Content/assets/css/ui.jqgrid.css" />

    <script src="../Scripts/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js"></script>
    <script src="../Scripts/knockout-3.1.0.debug.js"></script>
    <script src="../Content/lib/assets/js/bootstrap.min.js"></script>
    <script src="../Content/lib/assets/js/bootbox3/bootbox.min.js"></script>
    <script src="../Scripts/modernizr-2.6.2.min.js"></script>
    <script src="../Content/lib/assets/js/jquery.validate.min.js"></script>
    <script src="../Content/lib/assets/js/bootstrapValidator/bootstrapValidator.min.js"></script>

    <%--<script src="Scripts/jquery.jqGrid.min.js"></script>--%>
    <script src="../Scripts/modernizr-2.6.2.min.js"></script>
    <script src="../Content/lib/assets/js/jquery.jqGrid.min.js"></script>
    <script src="../Scripts/json2.min.js"></script>
    <script src="../Content/lib/assets/js/bootstrap-datepicker.min.js"></script>


    <script src="../Scripts/Common.js"></script>
    <script src="../Scripts/DataServices/CreditSourceDocs.js"></script>
    <script src="../Scripts/DataServices/StopPenalize.js"></script>
    <script src="../Scripts/DataServices/PISIQueue.js"></script>
    <script src="../Scripts/DataServices/BalanceReview.js"></script>

    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

<body class="no-skin">
    <form id="form1" runat="server" data-toggle="validator" class="form-horizontal" role="form">

HTML ChildPage

<div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                                        <h4 class="modal-title" id="btnSPStopGrid-label">Stop Parts Shipping Request</h4>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class='form-group'>
                                                            <span class="label label-default col-sm-7 col-sm-offset-2">Enter the reason and comments to stop the Shipping Request</span>
                                                            <br />
                                                            <label class="required col-sm-1 control-label" for="txtSPStatusComments">Status Comments:</label>
                                                            <div class="col-sm-9 col-sm-offset-1">
                                                                <textarea id="txtSPStatusComments" name="txtSPStatusComments" rows="5" cols="80" class="form-control height-auto" placeholder="Enter Comments"></textarea>
                                                            </div>
                                                            <div class="hide-text">
                                                                <input type="hidden" id="txtSPStopGridID" />
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" id="btnSPAcceptShippingReq" class="btn btn-primary">Accept</button>
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                                                    </div>
                                                </div>
                                            </div>

JS

$("#form1").validate({
        rules: {
            txtSPStatusComments: {
                required: true
            }
        },
        messages: {
            txtSPStatusComments: {
                required: "Status is a required field!"
            }
        },
        highlight: function (e)
        {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },
        unhighlight: function (e)
        {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
        },
        errorPlacement: function (error, element)
        {
            error.insertAfter(element.parent());
        }
    });
Community
  • 1
  • 1
sagesky36
  • 4,542
  • 19
  • 82
  • 130

1 Answers1

1

Quote OP:

"...sounds like the validate plugin (jquery.validate.min.js) can't be found in the cdn version of jQuery we are using."

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js"></script>

The root problem is that is not "the CDN version of jQuery"... that is the CDN version of jQuery UI and neither jQuery or jQuery UI would have the jQuery Validate plugin inside of it; nor would jQuery be inside of jQuery UI.

jQuery and the jQuery Validate plugin are two things you'll need to include separately from jQuery UI. See the CDN links on this page for the jQuery Validate plugin and this page for jQuery itself. Just remember to include jQuery before jQuery UI and any other jQuery plugins. (jQuery UI is considered as a jQuery plugin.)


Secondly, the .validate() method is used for initializing the plugin on your form, therefore is makes no sense to put it inside of a click handler. Once initialized, this plugin automatically captures the click event as long as the click comes from a type="submit" input or button.

Since you're using highlight, you should also be using unhighlight in place of success...

highlight: function (e) {
    $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
},
unhighlight: function (e) {  // opposite of highlight
    $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
}

You also do not need $(e).remove() because the plugin automatically removes the error message.


EDIT based on comments:

Include jQuery, then any jQuery plugins, then your jQuery/JavaScript code including the call to .validate()... this can be anywhere in the <head></head> section OR the <body></body> section... typically at the end.

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {  // ensure DOM is ready

        $("#form1").validate({  // initialize validate plugin
            ....
        });

    });
</script>

EDIT based on edits...

Quote OP:

As I mentioned before, I don't think the jQuery validate plugin works with bootstrap because it seems that the jQuery plugin requires "input type="submit" whereas bootstrap requires the following ("button type="submit"). If I change the "button type" to "input type" in the HTML, bootstrap doesn't accept this.

  • jQuery Bootstrap and the jQuery Bootstrap Validator plugin are two different things. To use the Bootstrap Validator plugin means you would need to also be using Bootstrap. Please be more mindful of the plugins and technologies you're trying to employ, as well as their individual dependencies.

  • You would not use the jQuery Validate and jQuery Bootstrap Validator plugins at the same time. Pick one and stick with it. You can't write code for one plugin and then expect it to work the same with a totally different plugin.

  • The jQuery Validate plugin works perfecting fine with jQuery Bootstrap. See: http://jsfiddle.net/dnfwq6bg/

  • The jQuery Validate plugin works perfectly fine with <button> OR <input> as long as it contains type="submit". See: http://jsfiddle.net/dnfwq6bg/1/

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Sparky, I totally understand... I tried both separately. I had no intention of combining them in the same project. I wanted to see which one I could get to work. Since the BootstrapValidator appears to have issues while validating two or more controls on the same horizontal line (meaning it's good for a top-down form with one control on each line), I've decided to use the jQuery Validate plugin. While I completely agree with your assessment and code of the jQuery Validate plugin, I still can't get it to work, despite your excellent input. – sagesky36 Dec 07 '14 at 15:29
  • I created a brand new project with only one input field and took the info from my MasterPage (note that I was forced to take over this project from another developer) and placed it in the new bare-bones project. I still can't get it to work. I'm thinking it maybe has something to do with the master page. I zipped it up and was hoping to email you the project just so you can see for yourself why the validation won't fire. If you could check that out, you should hopefully be able to physically see what I might be missing. I would appreciate it very much if I can email it to you. Thanks... – sagesky36 Dec 07 '14 at 15:34
  • @sagesky36, This jsFiddle shows it working with all of the code in your OP: http://jsfiddle.net/dnfwq6bg/2/ – Sparky Dec 07 '14 at 15:35
  • I agree, but it can't emulate what I have in the whole project with the file paths of the plugins, masterpage settings, etc. But this way, you have the whole project and I'm sure there's something in the project you could identify what's causing the validation not to fire. – sagesky36 Dec 07 '14 at 15:37
  • @sagesky36, My only tip would be that you remove all included scripts except for jQuery and jQuery Validate... then see how it works. Then try adding each plugin one at a time. – Sparky Dec 07 '14 at 15:38
  • Put `debug: true` into `.validate()` and then look at your JavaScript console. See: http://jsfiddle.net/dnfwq6bg/3/ – Sparky Dec 07 '14 at 15:39
  • Sparky, it DOES work with the bare bones project now. However, with our main project, it doesn't work. It must have something to do with the MasterPage scripts. I took out as much as I could without the project failing, but it still won't work. At this point, I'll have to send it to the LEAD developer in order to take a look at it. Thanks a lot for your time and input. – sagesky36 Dec 07 '14 at 16:48
  • @sagesky36, you'll have to employ some common sense troubleshooting... simply add each script **one at a time** until it breaks. – Sparky Dec 07 '14 at 16:51
  • Can't do it Sparky... There are too many scripts needed for the project to work. If I comment out the scripts at a bare-bones level, it won't work... – sagesky36 Dec 07 '14 at 17:01
  • @sagesky36, **that's not what I said!** If you want to know **which** script is breaking this, then you'll need to try each one... one at a time. Add another, try it... does it break? Then add another, try it... does it break? That's called "troubleshooting". – Sparky Dec 07 '14 at 17:10