BrunoLM
BrunoLM

Reputation: 100341

Why regex validation is not working?

I have a form with

<td class="label">@Html.LabelFor(u => u.Link)</td>
<td>
    @Html.TextBoxFor(u => u.Link)
    @Html.ValidationMessageFor(u => u.Link)
</td>

The Link property has a RegularExpression

[RegularExpression(@"^(([^\:\/?#]+)\:)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$", ErrorMessage= "invalid URL")]

But it is not being validated. If I add [Required] the field is validated.

I'm including 3 scripts:

Do I need to include another script? Why it doesn't work for Regex?

Upvotes: 1

Views: 384

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You probably have some error in the regex. Sorry, I can't spot it, regexes are a bit like Chinese to me (I know I am not helpful here)

I've tried the following and it worked fine:

[RegularExpression(@"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", ErrorMessage = "invalid URL")]

Upvotes: 2

Related Questions