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 .

tereško
  • 58,060
  • 25
  • 98
  • 150
Suraj K. Mad.
  • 31
  • 1
  • 5

1 Answers1

1

Try this:

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

function Submitform() {
    if (form.valid()) {
            $(form).submit();
    }
}
Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12
  • Here the only change is double quote from single quote, which is not going to help. Both are same, see [Here](http://stackoverflow.com/questions/3149192/difference-between-single-quotes-and-double-quotes-in-javascript) – PM. Dec 27 '13 at 12:53
  • Please look carefull `form.validate();` is the real change, you need to initialize validator before using it. – Dharmesh Patel Dec 27 '13 at 12:55
  • 2
    same error is coming form.validate() TypeError: form.validate is not a function – Suraj K. Mad. Dec 27 '13 at 13:26
  • Can you run $( "#frmAddStudent" ) in your browser's javascript console and verify that only one object is returned? – charles Dec 27 '13 at 23:36