xVal client-side validation not working

廉价感情. 提交于 2019-12-25 06:54:52

问题


I downloaded the xVal sample project from Steve Sanderson’s blog, run it, and the sever side validation it's working (after i click submit), but the client side doesn't; although i looked at the source and saw the generated javascript

<script type="text/javascript">xVal.AttachValidator("booking", {"Fields":[{"FieldName":"ClientName","FieldRules":[{"RuleName":"Required","RuleParameters":{}},{"RuleName":"StringLength","RuleParameters":{"MaxLength":"3"}}]},{"FieldName":"NumberOfGuests","FieldRules":[{"RuleName":"Range","RuleParameters":{"Min":"1","Max":"20","Type":"decimal"}},{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"ArrivalDate","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Date"}},{"RuleName":"Required","RuleParameters":{}}]}]})</script>

my view code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="CreateBooking.aspx.cs" Inherits="BookingsDemo.Views.Home.CreateBooking" %>
<%@ Import Namespace="DomainModel"%>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h1>Place a booking</h1>

    <% using(Html.BeginForm()) { %>
        <div>
            Your name: <%= Html.TextBox("booking.ClientName") %>
            <%= Html.ValidationMessage("booking.ClientName") %>
        </div>
        <div>
            Number of guests: <%= Html.TextBox("booking.NumberOfGuests")%>
            <%= Html.ValidationMessage("booking.NumberOfGuests")%>
        </div>
        <div>
            Expected arrival date: <%= Html.TextBox("booking.ArrivalDate")%>
            <%= Html.ValidationMessage("booking.ArrivalDate")%>
        </div>                
        <input type="submit" />       
    <% } %>
    <%= Html.ClientSideValidation<Booking>("booking") %>    
</asp:Content>

here is the generated html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>

</title><link href="../Content/Site.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="/Scripts/jquery-1.2.6.js"></script>
    <script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="/Scripts/xVal.jquery.validate.js"></script>
</head>

<body>
    <div class="page">

        <div id="header">
            <div id="title">
            </div>

            <div id="menucontainer">


            </div>
        </div>

        <div id="main">

    <h1>Place a booking</h1>
    <form action="/Home/CreateBooking" method="post">
        <div>
            Your name: <input id="booking_ClientName" name="booking.ClientName" type="text" value="" />

        </div>
        <div>
            Number of guests: <input id="booking_NumberOfGuests" name="booking.NumberOfGuests" type="text" value="" />


        </div>
        <div>
            Expected arrival date: <input id="booking_ArrivalDate" name="booking.ArrivalDate" type="text" value="" />

        </div>                

        <input type="submit" />       
    </form><script type="text/javascript">xVal.AttachValidator("booking", {"Fields":[{"FieldName":"ClientName","FieldRules":[{"RuleName":"Required","RuleParameters":{}},{"RuleName":"StringLength","RuleParameters":{"MaxLength":"15"}}]},{"FieldName":"NumberOfGuests","FieldRules":[{"RuleName":"Range","RuleParameters":{"Min":"1","Max":"20","Type":"decimal"}},{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]},{"FieldName":"ArrivalDate","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Date"}},{"RuleName":"Required","RuleParameters":{}}]}]})</script>


            <div id="footer">
                My Sample MVC Application &copy; Copyright 2008
            </div>

        </div>
    </div>
</body>
</html>

回答1:


the client side validation doesn't work in the demo downloaded from that blog, but with the new versions of jquery and xVal everything works fine :)



来源:https://stackoverflow.com/questions/1667863/xval-client-side-validation-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!