user1137472
user1137472

Reputation: 345

Regular Expression Validation

Hi guys I have looked a many versions of regular expression validation but I can not get it to work where it will only allow a certain letters at the beginning before anymore text is entered therefore if that text is not entered then nothing can be entered as it will show validation. For example I have tried:

[Required]
[RegularExpression("^(?:[www.]|a-z|A-Z|)*$", /*This allows the www. only and nothing else*/ ErrorMessage = "Cars Only")]
public string Cars { get; set; }

and i have tried:

[Required]
[RegularExpression("^(?:(ht|f)tp(s?))\://){1}\S+)|a-z|A-Z|)*$", /*This does not work*/
ErrorMessage = "Cars Only")]
public string Cars { get; set; }

Many others also but to much paste will become to long.

Can any help so after the www. anything can be typed in but if no www. is entered then it will show error message.

Upvotes: 1

Views: 1524

Answers (3)

Tim M.
Tim M.

Reputation: 54417

"www." matching:

www\..* // no line start/end criteria
^www\..*$ // line start/end enforced

Here's a more comprehensive expression that handles the prefix:

^(https?|ftp):\/\/www\..*$

This will match:

http://www.test.com 
https://www.test.com 
ftp://www.test.com/test

Better still, this makes the prefix mandatory, but the "www." optional:

^(https?|ftp)\:\/\/(www\.)?.*$

This will match:

http://www.test.com
https://www.test.com
ftp://www.test.com/test
http://test.com
https://test.com
ftp://foo.org

Matching images only

^(https?|ftp)\:\/\/(www\.)?.*\/[\w\-\+\%]*\.(gif|jpg|jpeg|png)$

This will match:

ftp://foo.org/test.png
https://test.com/image.jpg
http://www.bar.com/my-image.png
http://foo.org/my%20image.jpeg
http://foo.org/my%20image%20test_foo.jpeg

This will require a prefix at the beginning, a forward slash, a file name (which allows alphanumeric characters, underscores, dashes, and URL encoded values containing % or +) and a valid image extension.

This will not be foolproof! Remember that images can be served from any HTTP handler. The URL might have nothing image-related in it.

Additional Information

I agree with @Russ Cam's suggestion to look at the jQuery validator source code to see how many possibilities there are actually are.

It's possible that URL the user enters is in a valid form but doesn't exist; this could be checked server-side with a simple request.

I recommend RegexBuddy if you often need to create regular expressions.

I also recommend RegExr for quick online testing.

Upvotes: 3

Matt Lacey
Matt Lacey

Reputation: 8255

Split up the required parts (i.e. make the www. optional, and then what follows it optional but not dependent on the www.

[Required]
[RegularExpression("^(?:[www.])(?:|a-z|A-Z|)*$", /*This allows the www. only and nothing else*/ ErrorMessage = "Cars Only")]
public string Cars { get; set; }

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125538

according to your criteria,

[Required]
[RegularExpression(@"^www\..+$", ErrorMessage = "Cars Only")]
public string Cars { get; set; }

You can play with this regex on nregex to see what is matched and what isn't.

I suspect your looking for Regular Expression to match a valid URI? If you are, I would use a regex for the client side (take a look at the url rule in jQuery validate) and create a Validation attribute for the server side that passes the string into Uri.IsWellFormedUriString(string uri, UriKind.Absolute)

Upvotes: 0

Related Questions