C#表单中的JavaScript表单验证

时间:2014-12-19 19:30:29

标签: javascript c#

我需要使用JavaScript进行表单验证才能生成所需的字段。我用来检查输入字段的脚本需要表单名称和字段名称。我尝试修改Html.BeginForm以包含表单名称,但现在提交的内容不起作用,它会显示" System.Web.Mvc.Html.MvcForm {"在页面上。

 @using (Html.BeginForm(null, null, FormMethod.Post, new { id="frm", name = "frm"} )) {

 @Html.ValidationSummary(true)

<div class="editor-label">
    @Html.LabelFor(model => model.PNTR_MAX)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.PNTR_MAX)<br /><font style="color: red;">
    @Html.ValidationMessageFor(model => model.PNTR_MAX)</font>
</div>

<script>
    function validateForm() {
        var x = document.forms["frm"]["PNTR_MAX"].value;
        if (x == null || x == "" || x == "    ") {
            newcontent.innerHTML = "Error: PNTR Max is required";
            return false;
        }
    }
 </script>

更新输入现在成功提交和更新,但表单验证不起作用/做任何事情。

@using (Html.BeginForm("Edit", "Table9_1", FormMethod.Post, new { onsubmit = "validateForm", id="frm", name = "frm"} )) {

@Html.ValidationSummary(true)

<div class="editor-label">
    @Html.LabelFor(model => model.PNTR_MAX)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.PNTR_MAX)<br /><font style="color: red;">
    @Html.ValidationMessageFor(model => model.PNTR_MAX)</font>
</div>
<p id="demo"></p>

<script>
    function validateForm() {
        var x = document.forms["frm"]["PNTR_MAX"].value;
        if (x == null || x == "" || x == "    ") {
            document.getElementById("demo").innerHTML = "Error: PNTR Max is required";
            return false;
        }
    }
</script>

0 个答案:

没有答案