开发者

Client side validation only half works, works for a textarea, not a textbox

Let me start by explaining my situation. Right now the server side validation for my form works, and now I am trying to get the client side validation working for my form. Currently my form has both textboxs and a textarea, but only the text area is using the client side validation, none of my textboxs are. If I hit the submit button, using fiddler I can see it is still calling the server to validate the form. Can anyone please help me understand why the client side validation will work for my textarea, but won't work for my textboxs. I am very new to asp.net and C# so any help will be greatly appreciated.

I have looked on several sites so far for help:

MVC 2 Client side validation is not working

Client side validation - Pietro Brambati's Blog

Haacked.com - MVC 2 Custom Validation

Scott Gu's Blog - Model Validation

and several others which I just have already closed the tabs on. I apologize for not being able to make them all links, but since this is my first post, I am only allowed one link. Here is my code,

From the master page:

<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript"></script>

From the Form:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HaveAQuestion.ascx.cs"
Inherits="ViewUserControl<HaveAQuestionViewModel>" %>
<% Html.EnableClientValidation(); %>
<div id="HaveAQuestion">
<%--<form method="post" action="<%= Url.Action("SendMessage", "Home") %>">--%>
<%using (Html.BeginForm("SendMessage", "Home", FormMethod.Post))
  {%>
<fieldset id="signin_menu" <% if (!ViewData.ModelState.IsValid) {%>
    style="display: block;"
<%} %> >
    <div style="padding: 25px">
        <div class="FieldTitle">
            First & Last Name
            <%=Html.ValidationMessageFor(a => a.Name)%>
            </div>
        <div class="FieldArea">
            <%--<input id="username" name="username" value="" class="Fields" title="username" type="text">--%>
            <%= Html.TextBox("name", Model.Name, new { id = "name", title = "name", @class = "Fields" })%>
        </div>
        <div class="FieldTitle">
            Phone
            <%=Html.ValidationMessageFor(a => a.Phone)%>
            </div>
        <div cl开发者_Go百科ass="FieldArea">
            <%--<input id="username" name="username" value="" class="Fields" title="username" type="text">--%>
            <%= Html.TextBox("phone", Model.Phone, new { id = "phone", title = "phone", @class = "Fields" })%>
        </div>
        <div class="FieldTitle">
            Email
            <%=Html.ValidationMessageFor(a => a.Email)%>
         </div>
        <div class="FieldArea">
            <%--<input id="username" name="username" value="" class="Fields" title="username" type="text">--%>
            <%= Html.TextBox("email", Model.Email, new { id = "email", title = "email", @class = "Fields" })%>
        </div>
        <div class="FieldTitle">
            Comments/Questions
            <%=Html.ValidationMessageFor(a => a.CommentsOrQuestions)%>
            </div>
        <div class="TextArea">
            <%= Html.TextArea("CommentsOrQuestions", Model.CommentsOrQuestions, 4, 100, new { id = "CommentsOrQuestions", title = "comments", @class = "TextAreaField" })%>
        </div>
        <div style="padding-top: 10px">           
            <%= Html.ValidationMessage("error")%>
            <% if (!ViewData.ModelState.IsValid)
               {%><br /><%} %>                
            <input type="submit" value="Submit"/>
        </div>
</fieldset>
<%} %>
<%--</form>--%>

The View Model Code (As you can see the first [Required] tag has AllowEmptyString in it which I had read from a different article, and to show you that I have tested it):

public class HaveAQuestionViewModel
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "*")]
    public string Name { get; set; }

    [Required(ErrorMessage = "*")]
    //this is my own regular expression that I created
    [RegularExpression("^[a-zA-Z0-9]+[\\.a-zA-Z0-9_]*[@][.a-zA-Z0-9]+[.]([a-z]{2,4})$", ErrorMessage = "*")]

    public string Email { get; set; }

    [Required(ErrorMessage = "*")]
    // Will take ddd-ddd-dddd or (ddd)ddd-dddd, or (ddd)-ddd-dddd
    [RegularExpression("[(]*([0-9]{3})[)]*[-]*([0-9]{3})[-]([0-9]{4})$" , ErrorMessage = "*")]
    public string Phone { get; set; }

    [Required(ErrorMessage = "*")]
    public string CommentsOrQuestions { get; set; }
}

My Controller (this doesn't matter because it is suppose to validate everything on the client side.

The script being produced from <% Html.EnableClientValidation(); %>

<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) 
{ window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push(
{"Fields":[{"FieldName":"Name","ReplaceValidationMessageContents":true,
"ValidationMessageId":"Name_validationMessage",
"ValidationRules":[{"ErrorMessage":"*","ValidationParameters":    },"ValidationType":"required"}]},
{"FieldName":"Phone","ReplaceValidationMessageContents":true,
"ValidationMessageId":"Phone_validationMessage",
"ValidationRules":[{"ErrorMessage":"*","ValidationParameters":{"pattern":"[(]*([0-9]  {3})[)]*[-]*([0-9]{3})[-]([0-9]{4})$"},
"ValidationType":"regularExpression"},
{"ErrorMessage":"*","ValidationParameters":{},"ValidationType":"required"}]},
{"FieldName":"Email","ReplaceValidationMessageContents":true,
"ValidationMessageId":"Email_validationMessage",
"ValidationRules":[{"ErrorMessage":"*","ValidationParameters":{"pattern":"^[a-zA-Z0-9]+[\\.a-zA-Z0-9_]*[@][.a-zA-Z0-9]+[.]([a-z]{2,4})$"},
"ValidationType":"regularExpression"},{"ErrorMessage":"*","ValidationParameters":{},
"ValidationType":"required"}]},
{"FieldName":"CommentsOrQuestions","ReplaceValidationMessageContents":true,
"ValidationMessageId":"CommentsOrQuestions_validationMessage",
"ValidationRules":[{"ErrorMessage":"*","ValidationParameters": {},"ValidationType":"required"}]},
{"FieldName":"error","ReplaceValidationMessageContents":true,
"ValidationMessageId":"error_validationMessage","ValidationRules":  []}],"FormId":"form0","ReplaceValidationSummary":false});
//]]>
</script>

Please help me, I'm running VS 2010, and using ASP.NET MVC 2.0. For some reason the text area will work while none of my text boxes do.


Try capitalizing the id of each texbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜