简体   繁体   English

使用JavaScript的ASP.NET MVC表单验证

[英]ASP.NET MVC Form Validation using JavaScript

>" %> >“%>

Index 指数

 <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript" src="/Scripts/jquery.validate.js"></script> <script type="text/javascript"> input[type=text] input[type=checkbox] </script> <h2> Todays Cases</h2> <div> <%= Html.Encode(ViewData["message"]) %> </div> <form action="/TodaysCases/Index" method="post" id="rxForm"> <table> <% foreach (var item in Model) { %> <tr> <td> <%= Html.Encode(item.LastName+", "+item.FirstName) %> </td> <td> <%= Html.Encode(item.Age) %> </td> <td> <input type="checkbox" name="NoComments" id="<%= item.CaseNumber%>" /> </td> <td> <input type="text" name="Comments" id="comment_<%= item.CaseNumber%>" /> </td> <td> <label> Yes </label> </td> <td> <input name="CaseNumber" type="hidden" value="<%=item.CaseNumber %>" /> </td> </tr> <% } %> </table> <input type="submit" value="Sign" /> </form> 

In my model depending on how many cases are scheduled for today I may have one and upto 15 cases. 在我的模型中,根据今天要安排的案件数量,我可能有一个,最多15个案件。 So the page when displayed will have upto 15 rows. 因此,该页面在显示时最多有15行。 Each row will have a check box and a comment field. 每行将有一个复选框和一个注释字段。 What I want to achieve with Java Script is the client side validation. 我想用Java Script实现的是客户端验证。 Of course I will do the server side validation as well. 当然,我也会进行服务器端验证。 If the box is checked, then it means no comments, so I want to clear the corresponding comment field. 如果选中此框,则表示没有评论,因此我想清除相应的评论字段。 if the box is unchecked, I want to force the user to have put some comment before submission of the form. 如果未选中该框,则我想强制用户在提交表单之前添加一些评论。 Basically if there is some comment the user puts some comments. 基本上,如果有评论,用户会发表一些评论。 If no comments he check the box, ie, take some action. 如果没有评论,则选中该框,即采取一些措施。 How do I go about doing this in Javascript? 我该如何使用Javascript执行此操作? I learnt jQuery thinking I could do it there but I could not figure it out. 我学会了jQuery,认为我可以在那做,但我不知道。 Any help is appreciated. 任何帮助表示赞赏。

If you are using ASP.NET MVC 2.0 Beta, you can take advantage of the built-in, client-side validation that it provides. 如果使用的是ASP.NET MVC 2.0 Beta,则可以利用它提供的内置客户端验证。

http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

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

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