简体   繁体   English

使用javascript在MVC3中验证表单

[英]validating form in MVC3 using javascript

I just need example for one validation. 我只需要一个验证的例子。 For remaining I will do. 对于剩下的我会做。 Let's say I have an input of type text: 假设我输入了类型文本:

 <p>                          
<label for="ClientName">ClientName:</label> <%= Html.TextBoxFor(model => model.Name)%>         
 </p>        

Here I want to validate textbox for required field. 在这里,我想验证所需字段的文本框。 I want this required field validation function in JavaScript and I want to use this script in the view. 我想在JavaScript中使用这个必需的字段验证功能,我想在视图中使用这个脚本。

Have you considered using unobtrusive validation? 您是否考虑过使用不引人注目的验证?

You say you are using MVC3 (although not the Razor view engine apparently). 你说你正在使用MVC3(虽然显然不是Razor视图引擎)。

With your code like this: <p><label for="ClientName">ClientName:</label> <%= Html.TextBoxFor(model => model.Name)%></p> which could be written as <p>@Html.LabelFor(model=>model.Name) @Html.TextBoxFor(model => model.Name) </p> in Razor syntax. 使用这样的代码: <p><label for="ClientName">ClientName:</label> <%= Html.TextBoxFor(model => model.Name)%></p> ,可以写成<p>@Html.LabelFor(model=>model.Name) @Html.TextBoxFor(model => model.Name) </p>在Razor语法中。

If you put this in your web.config: 如果你把它放在web.config中:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

and then decorate the property in your model with something like this data annotation: 然后使用类似这样的数据注释来装饰模型中的属性:

[Display(Name = "Name")]
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }

Then by adding the following into your _Layout.cshtml file you will get unobtrusive validation to work: 然后通过将以下内容添加到_Layout.cshtml文件中,您将获得不显眼的验证工作:

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.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>

Then you could add this somewhere on the page (where you want the validation message to be shown): @Html.ValidationMessageFor(model=>model.Name) 然后你可以在页面上的某处添加它(你想要显示验证消息的地方): @Html.ValidationMessageFor(model=>model.Name)

Try this code.it work on my side you should try: 试试这个代码。在我这边工作你应该尝试:

 <form id="formname" post="" action="">
<ul>
<li>
 <input type="text" name="TimeArrived" id="TimeArrived" class="required" /></li>
 <li><input type="text" name="Name" id="Name" class="required"   />
</li>
 <li>
    <input type="button" name="Save" value="Save" onclick="return Submit();" /></li>
   </ul>
</form>

 $(document).ready(function () {
        var frm = $("#formname");
        frm.validate();
    });

 function Submit() {
  var f = $("#formname");

 if (f.valid())
{
}
}

Now <%= Html.TextBoxFor(model => model.Name)%> is the key here u should give an ID to each of your elements that you want to use validation on.. 现在<%= Html.TextBoxFor(model => model.Name)%>是关键,你应该为你想要使用验证的每个元素提供一个ID。

so its gonna look like; 它看起来像;

<%= Html.TextBoxFor(model => model.Name,new { id="ClientNameTxt"})%>

if you already defined some scripts (.js files) and want to implement to this view then use 如果您已经定义了一些脚本(.js文件)并希望实现到此视图,那么请使用

<script src="@Url.Content("~/Scripts/yourjsfile.js")" type="text/javascript"></script>

and use ur functions to validate or else write new script 并使用ur函数来验证或编写新脚本

<script type="text/javascript">
    $(document).ready(function () {
        var frm = $("#formname");
        frm.validate();
    });

    $('#formname').Submit(function (event) {
        /* Call ur form with the id you have given */
        var f = $("#formname");

        if (f.valid()) {  /* When the form is valid */

        } else {  /* When the form is not valid */
            event.preventDefault(); /* Prevent from submitting the form */
            $("#ClientNameTxt").highlight(); /* Do some validation stuff on ur validation required element */
        }
    });
</script>

at the end it will look like; 最后看起来像;

                 <!--Your form name, Controller name-->         
@using (Html.BeginForm("Formname", "ControllerName", FormMethod.Post,new { id="Formname", onkeypress="return event.keyCode != 13;"}))
{
    <p> 
        <label for="ClientName">ClientName:</label> <!--Give an ID to your element-->
        <%= Html.TextBoxFor(model => model.Name, new { id="ClientNameTxt" })%>
    </p>
}

<script type="text/javascript">
    $(document).ready(function () {
        var frm = $("#formname");
        frm.validate();
    });

    $('#formname').Submit(function (event) {
        /* Call ur form with the id you have given */
        var f = $("#formname");

        if (f.valid()) {  /* When the form is valid */

        } else {   /* When the form is not valid */
            event.preventDefault(); /* Prevent from submitting the form */
            $("#ClientNameTxt").highlight(); /* Do some validation stuff on ur validation required element */
            $("#Formname").append('<ul><li> Fill the empty areas and try again. <li><ul>');
            /* This is the worst i can do. Just to let you understand it easly. */
        }
    });
</script>

if u still have problems with this issue! 如果你仍然有这个问题的问题! i guess ur solution is at training your self with the --> jquery 我想你的解决方案是用 - > jquery来训练你的自我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM