2

I am creating a simple mvc3 application.

the VIew is

<script>
function Submitform() {
    var form = $('#frmAddStudent');
    if (form) {
        if (form.valid()) {
            $(form).submit();
        }
    }
}
</script>

@using (Ajax.BeginForm("AddStudent", "Student", new AjaxOptions { HttpMethod = "POST" }, new { id = "frmAddStudent" }))
{
@Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.StudentName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.StudentName)
        @Html.ValidationMessageFor(model => model.StudentName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address)
        @Html.ValidationMessageFor(model => model.Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.DOB)
    </div>
    <div class="editor-field">
        @Html.Telerik().DatePickerFor(model => model.DOB)
        @Html.ValidationMessageFor(model => model.DOB)
    </div>
<p>
   <input type="button" value="Add" class="btn" onclick="return Submitform()" />
    <input type="button" value="Cancel" class="close btn" onclick="closeDialog()" />
    </p>
}

now the Problem is when i click on Submit button javascript function calls and it doesn't recognize "form.valid()" function.

it displays form.valid() TypeError: form.valid is not a function

so, whats the Problem in this .

2
  • What all JS files you have included in your view? Can you post that part of code? Commented Dec 27, 2013 at 12:28
  • <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> Commented Dec 27, 2013 at 12:30

1 Answer 1

1

Try this:

var form = $( "#frmAddStudent" );
form.validate();

function Submitform() {
    if (form.valid()) {
            $(form).submit();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Here the only change is double quote from single quote, which is not going to help. Both are same, see Here
Please look carefull form.validate(); is the real change, you need to initialize validator before using it.
same error is coming form.validate() TypeError: form.validate is not a function
Can you run $( "#frmAddStudent" ) in your browser's javascript console and verify that only one object is returned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.