maxshuty
maxshuty

Reputation: 10662

Why wont jQuery Validate work?

I have ran through the docs and can't figure out where I'm going wrong. jQuery Validate wont work at all. In Chrome dev tools I have no errors or anything, so I'm not sure what I'm doing wrong.

My view follows this format:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "eventForm" })) {
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EventName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EventName, new { htmlAttributes = new { @class = "form-control", name="eventName", id="eventName" } })
                @Html.ValidationMessageFor(model => model.EventName, "", new { @class = "text-danger" })
            </div>
        </div>
 }

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script src="~/scripts/jquery.datetimepicker.full.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script type="text/javascript">

    $('#eventForm').validate({
        rules: {
            eventName: {
                required: true
            }
        },
        messages: {
            eventName: "Event name is required"
        }
    });
</script>

I added name and id to all of my elements in the view that I want validated. Where could I be going wrong? I have no errors or anything like that so I'm not entirely sure? Do I need to include another <script>?

Upvotes: 0

Views: 54

Answers (1)

tanaydin
tanaydin

Reputation: 5316

You have to trigger such as things in page load... like so...

$(document).ready(function(){
    $('#eventForm').validate({
        rules: {
            eventName: {
                required: true
            }
        },
        messages: {
            eventName: "Event name is required"
        }
    });
});

Upvotes: 2

Related Questions