Working with with Webmatrix. Starter Site has form validation methods. I thought I had this under control until I went to validate a first name field in a form.
I can keep the form clean on zipcode with this validation:
Validation.Add("zipcode",Validator.Required("You must supply a zipcode"),Validator.Regex(@"^\d{5}$","Enter 5 digit zip code e.g. 46060"));
It works perfectly. A blank/empty zipcode gets the Required message and anything other than 5 digits fails with the second message.
So, to keep the user from entering symbols or anything other than a-Z or 0-9 in the first name text box I set the following Regex pattern.
Validation.Add("fname",
Validator.Required("You must supply a first name"),
Validator.Regex(@"^[a-zA-Z0-9]$","First name?") );
A blank/empty fname result in the first message. The only text that works is a single character. a through Z or 0 through 9. It seems that maybe a length attribute might be lurking in the regex engine shadows. R (as single character) passes the validation
RW (more than a single character) doesn't work.
Wow, no place on the web (after 15 RegEx cheat-sheets) does anyone suggest a length attribute is required. OK maybe it is required in Webmatrix. Thanks.
I expanded the validation on the first name textbox. The idea being to keep users from attempting scripting or typo's on a first name field.
/*Validation.RequireField("fname", "You must specify a first name."); */
Validation.Add("fname",Validator.Required("You must supply a first name"),Validator.Regex(@"^[a-zA-Z0-9]{1,15}$","Math symbols are not valid."));
/* add size="21" maxsize="15" on <input> tag for alignment and to match with the length attribute on Regex()*/
for the next newbee
/* @ let Razor processing this command.
^ start the pattern
[ start explicit to match
a-zA-Z0-9 filter for any characters lower a-z or upper A-Z and any numbers 0-9
] end the match
{1,15} set the scan length from 1 to 15 characters (default is 1)
Wow, no place on the web (after 15 RegEx cheat-sheets) does anyone suggest a length attribute is required. OK maybe it is required in Webmatrix.
Cheat sheets won't "suggest" how to produce a Regex. They will simply provide explanations as to what metacharacters and quantifiers are available. It's up to you to learn how to apply those to meet your needs.
The actual explanation for the Regex is as follows:
@ the following should be treated as a literal C# string (nothing to do with Razor)
^ start of line or string
[a-zA-Z0-9] any characters in this character set (lowercase or upper case alpha characters and digits
{1,15} minimum 1 occurrence, maximum 15 occurrences
$ end of line or string.
DMT20601
Member
86 Points
197 Posts
Webmatrix Regex validation not consistent.
Jan 04, 2013 08:17 PM|LINK
Working with with Webmatrix. Starter Site has form validation methods. I thought I had this under control until I went to validate a first name field in a form.
I can keep the form clean on zipcode with this validation:
It works perfectly. A blank/empty zipcode gets the Required message and anything other than 5 digits fails with the second message.
So, to keep the user from entering symbols or anything other than a-Z or 0-9 in the first name text box I set the following Regex pattern.
Validation.Add("fname", Validator.Required("You must supply a first name"), Validator.Regex(@"^[a-zA-Z0-9]$","First name?") );A blank/empty fname result in the first message. The only text that works is a single character. a through Z or 0 through 9. It seems that maybe a length attribute might be lurking in the regex engine shadows. R (as single character) passes the validation RW (more than a single character) doesn't work.
Thanks
Dallas in Maryland
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: Webmatrix Regex validation not consistent.
Jan 04, 2013 09:42 PM|LINK
You need to add a quantifier to the fname regex.
^[a-zA-Z0-9]{1,15}$will match between 1 and 15 letters or numbers.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
DMT20601
Member
86 Points
197 Posts
Re: Webmatrix Regex validation not consistent.
Jan 06, 2013 04:37 PM|LINK
@#$%^&*
Wow, no place on the web (after 15 RegEx cheat-sheets) does anyone suggest a length attribute is required. OK maybe it is required in Webmatrix. Thanks.
I expanded the validation on the first name textbox. The idea being to keep users from attempting scripting or typo's on a first name field.
/*Validation.RequireField("fname", "You must specify a first name."); */Validation.Add("fname", Validator.Required("You must supply a first name"), Validator.Regex(@"^[a-zA-Z0-9]{1,15}$","Math symbols are not valid.") );/* @ let Razor processing this command.^ start the pattern[ start explicit to matcha-zA-Z0-9 filter for any characters lower a-z or upper A-Z and any numbers 0-9] end the match{1,15} set the scan length from 1 to 15 characters (default is 1)$ end the patternMikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: Webmatrix Regex validation not consistent.
Jan 06, 2013 04:50 PM|LINK
Cheat sheets won't "suggest" how to produce a Regex. They will simply provide explanations as to what metacharacters and quantifiers are available. It's up to you to learn how to apply those to meet your needs.
The actual explanation for the Regex is as follows:
@ the following should be treated as a literal C# string (nothing to do with Razor)
^ start of line or string
[a-zA-Z0-9] any characters in this character set (lowercase or upper case alpha characters and digits
{1,15} minimum 1 occurrence, maximum 15 occurrences
$ end of line or string.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter