ASP.NET MVC 2 client-side validation triggering incorrectly

I am using DataAnnotations to enable client-side validation in an ASP.NET MVC 2 project. I am having an issue where my URL validation regex passes my unit test, but it fails in the actual website.

[RegularExpression(UrlValidation.Regex, ErrorMessage = UrlValidation.Message)]
public string Url { get; set; }

Regex = "(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"

Message = "Invalid Url"

<div class="editor-label">
    <%: Html.LabelFor(model => model.Url) %>
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.Url) %>
    <%: Html.ValidationMessageFor(model => model.Url) %>

Result With URL =,8

Passing Unit Test
public void GetVarUrlPasses()
    var url = ",8";
    var regex = new Regex(UrlValidation.Regex);

Does anyone have any idea why this is passing the unit test, but failing when I test the view in a browser?


I can see several problems with that regex, but what's tripping you up is probably the comma in p=7,8. Your regex doesn't match it, but IsMatch doesn't require it to; it's perfectly happy stopping at the 7. I would guess that the client-side validator is implicitly anchoring the match. The regex should be anchored anyway; add a ^ to the beginning and $ to the end, and you should at least get consistent results. Then you can change the regex to accommodate the comma.

There's also a typo in the regex: the second f in [a-fA-f\d] should be F. And, although it's not an error to have \d and \w in the same set of square brackets, it is redundant; \w matches digits as well as letters, so you can remove the \d (and [\w\d] can be reduced to \w). Finally, {2,2} should be simply {2}.

Need Your Help

How can i set li tag with extra characters?

html css

i have a design style you can see in :

Crontab issue with perl script

perl crontab sql-loader

system("$sqlldr $oracleLogon control=$ctlFile log=$logFile data=$dateFile");