Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:46:59+00:00 2026-05-30T02:46:59+00:00

The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the

  • 0

The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.

Model:

[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }

Razor View:

@Html.TextBoxFor(model => Model.FirstName, new { })
@Html.ValidationMessageFor(model => Model.FirstName)

The unobtrusive validation is rendered in view as:

<input type="text" value="" tabindex="1" style="height:auto;" name="FirstName" maxlength="100" id="FirstName" data-val-regex-pattern="^([a-zA-Z0-9 .&amp;amp;&amp;#39;-]+)$" data-val-regex="Invalid First Name" data-val-length-max="100" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val="true" class="textfield ui-input-text ui-body-d ui-corner-all ui-shadow-inset valid">

The regex pattern in the above html is not rendered as specified in the Model’s RegularExpression, which results in error even when entering the valid data (Sam's).

How can i handle this?

–UPDATE–

I have updated the code as per @Rick suggestion

[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
public string FirstName { get; set; }

View Source shows the following:

<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val-length-max="100" data-val-regex="Enter only alphabets and numbers of First Name" data-val-regex-pattern="([a-zA-Z0-9 .&amp;amp;&amp;#39;-]+)" id="FirstName" maxlength="100" name="FirstName" type="text" value="" />

Still i have the same issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:47:01+00:00Added an answer on May 30, 2026 at 2:47 am

    UPDATE 9 July 2012 – Looks like this is fixed in RTM.

    1. We already imply ^ and $ so you don’t need to add them. (It doesn’t appear to be a problem to include them, but you don’t need them)
    2. This appears to be a bug in ASP.NET MVC 4/Preview/Beta. I’ve opened a bug

    View source shows the following:

    data-val-regex-pattern="([a-zA-Z0-9 .&amp;&#39;-]+)"                  <-- MVC 3
    data-val-regex-pattern="([a-zA-Z0-9&#32;.&amp;amp;&amp;#39;-]+)"      <-- MVC 4/Beta
    

    It looks like we’re double encoding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've been working with ASP.Net MVC2 based web app. We're using DataAnnotations for model
I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model
As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC
I have an ASP.NET MVC 2 form that is working perfectly, doing client side
I'm decorated a ViewModel in my ASP.NET MVC 2 site with System.ComponentModel.DataAnnotations validation attributes.
I'm using the Data Annotation validation extensively in ASP.NET MVC 2. This new feature
I am using asp.net mvc 3, jquery validate, unobstructed javascript and data annotations. I
I'm using asp.net mvc 3 with jquery unobtrusive validation. I recently changed from standard
I am using EF4 and ASP.NET MVC 2 for my web application. I am
In my asp mvc application I use standard client side validation (DataAnnotations + MicrosoftAjax.js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.