0

I have been stuck on this issue since a week. I am not able to validate bootstrap modal using jQuery validator function. I have searched lots of stuff, also I have used exactly the same method as mentioned in here: How to correctly validate a modal form . I have been using django framework. Here is my HTML code:

<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog" 
                 aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <!-- Modal Header -->
                        <form class="form-horizontal" id="myForm" role="form" action="{% url 'apply:add' %}" method="POST">
                            {% csrf_token %}
                        <div class="modal-header">
                            <button type="button" class="close" 
                               data-dismiss="modal">
                                   <span aria-hidden="true">&times;</span>
                                   <span class="sr-only">Close</span>
                            </button>
                            <h4 class="modal-title" id="myModalLabel">
                                Apply Leave
                            </h4>
                        </div>

                        <!-- Modal Body -->
                        <div class="modal-body">
                                    <div class="form-group">
                                        <label  class="control-label col-sm-3" for="e_id">Employee-Id</label>
                                        <div class="col-sm-9">
                                            <select class="form-control" id="e_id" name="e_id" class="required">
                                                {% for entry in emp_data %}
                                                    <option value="{{ entry.emp_id }}">{{ entry.emp_id }} - {{ entry.emp_name}} </option>
                                                {% endfor %}
                                            </select>
                                        </div>
                                    </div>
                                    <style>
                                    .datepicker {
                                      z-index: 1600 !important;
                                      border-color: black;

                                    }
                                    </style>

                                    <div class="form-group">
                                        <label  class=" control-label col-sm-3" for="s_dt">Start Date</label>
                                            <div class="col-sm-9" >
                                            <input type='text' class="form-control" id="s_dt" placeholder="Start Date" name="s_dt"/>
                                            </div>
                                    </div>


                                    <div class="form-group">
                                        <label  class="control-label col-sm-3" for="e_dt">End Date</label>
                                            <div class="col-sm-9" >
                                            <input type='text' class="form-control" id="e_dt" placeholder="End Date" name="e_dt" require/>
                                            </div>
                                    </div>

                                  <p class="col-sm-9 col-sm-offset-3" > Enter Start Date = End Date if leave is applied for a single day </br></p>

                                    <div class="form-group">
                                        <label  class="col-sm-3 control-label" for="l_type">Leave Type</label>
                                        <div class="col-sm-9">
                                                <select class="form-control" id="l_type" name="l_type">
                                                    <option value="S">Sick Leave</option>
                                                    <option value="V">Vacation Leave</option>
                                                    <option value="M">Maternity Leave</option>
                                                    <option value="P">Paternity Leave</option>
                                                    <option value="C">Casual Leave</option>
                                                </select>

                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label" for="desc">Description</label>
                                        <div class="col-sm-9">
                                            <textarea class="form-control" id="desc" rows="3"></textarea>
                                        </div>
                                    </div>

                                  <!-- Modal Footer -->
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default"
                                                data-dismiss="modal">
                                                    Close
                                        </button>
                                        <button type="submit" class="btn btn-primary" name="save" id="save" value="Save changes" data-dismiss="modal">
                                            Save changes
                                        </button>
                                    </div>
                            </form>      
                        </div>  
                    </div>
                </div>
            </div>

Here is my javascript:

  $(function() {
  $("#myForm").validate({
        rules: {
          s_dt: "required",
          e_dt: "required"

        },
       messages: {
          s_dt: "Please provide a date",
          e_dt: "Please provide a date"
        }
      });
    });

Thanks in advance..

Ravi Shankar
  • 277
  • 9
  • 23
  • make sure you have added all related js file to your page and sequence is same as this ` ` – Curiousdev Aug 29 '17 at 12:38
  • @Curiousdev If you are familiar with django, here is what i have done: I have a base.html file in which apply.html is being called. All the scripts, I have added them in my base.html. And I have added the above js files at the end of all my scripts. Still, it is not working.. ( base.html file https://dpaste.de/CTHo#L ) – Ravi Shankar Aug 29 '17 at 13:00
  • I even get "Failed to load resource" error while loading scripts on my console. – Ravi Shankar Aug 29 '17 at 13:22
  • as i have seen your file and found there are multiple same js file has been loaded in you file remove unnecessary duplicate js files from your html and follow a sequence of js files as i have added in my previous comment and after doing all this stuff make sure your console gets clean with no errors there and than check it'll work there is nothing to do with your above js code it's completely fine – Curiousdev Aug 29 '17 at 13:33
  • additionally put this `jquery-1.10.2.min.js'` js on top of all JS – Curiousdev Aug 29 '17 at 13:34
  • Can you please help me with the order and redundancy.. When im trying order them, new problems are arising.. https://dpaste.de/JH9u#L : base.html – Ravi Shankar Aug 30 '17 at 04:42
  • try this one [LINK](https://dpaste.de/b56E#L) and make sure your console errors has been clear i have just ordering js in sequence and removed duplicate js – Curiousdev Aug 30 '17 at 05:12
  • Thanks a lot for that, but it still doesn't work. I get " GET https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jqu%E2%80%8C%E2%80%8Bery.min.js (index):1451 " kind of errors on my console. What is the issue? – Ravi Shankar Aug 30 '17 at 06:34
  • replace thi to `https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js` in your index page – Curiousdev Aug 30 '17 at 06:41
  • It is the same ... in my code – Ravi Shankar Aug 30 '17 at 06:44
  • you need to show in somewhere like hosted it somewhere so i can figure out what's actually going wrong from this side i can't assume what's going wrong :( – Curiousdev Aug 30 '17 at 06:47
  • Here is updated base.html https://dpaste.de/9E6g#L and here is screenshot of the error: https://pasteboard.co/GI0EAae.png .. Is this okay? – Ravi Shankar Aug 30 '17 at 06:58
  • search for `??` in your file something wrong in that remove all `??` from source url of any js – Curiousdev Aug 30 '17 at 07:02
  • no `??` anywhere.. :/ – Ravi Shankar Aug 30 '17 at 07:14
  • As per your error screenshot it seems some invalid characters in your source js url double check them something wrong there – Curiousdev Aug 30 '17 at 07:16
  • Well, for this, I have removed all the urls and downloaded them and given the path for respective files. Error still persists Screenshot here [link](https://pasteboard.co/GI0R13D.png) and base.html here: https://dpaste.de/jZs6#L – Ravi Shankar Aug 30 '17 at 07:29

0 Answers0