I'm developing a webmatrix website using the Starter Site template. The register.cshtml demonstrates several validation features.
My question (1) is this client-side validation or server-side validation within webmatrix or both?
All the Webmatrix reading suggests client-side is nice but not dependable, therefore server-side is a must.
----------------
I need to add a zipcode to the form
(2) ... how do limit it's length to 5 numbers in the validation.
Validation.RequireField("zipcode","You must specify a zipcode.");
<liclass="zipcode"><labelfor="zipcode"@if(!ModelState.IsValidField("zipcode")){<text>class="error-label"</text>}>Zipcode</label><inputtype="text"id="zipcode"name="zipcode"value="@zipcode"@Validation.For("zipcode")/>@* Write any zipcode validation errors to the page *@@Html.ValidationMessage("zipcode")</li>
Your validation is likely to be both client-side and server-side. Server-side is required because your server is receiving information from the Internet, thus automatically making it (your request, not the client-side validation) not dependable. The client-side
validation is a bonus to prevent unnecessary posts to the server if the data is not valid to begin with.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
This code works in Webmatrix to validate a forms zipcode, but not for international zipcodes.
Var zipcode=""
Validation.RequireField("zipcode","You must specify a zipcode.");Validation.Add("zipcode",Validator.StringLength(minLength:05,maxLength:05,errorMessage:"Zipcode must be 5 numbers"));Validation.Add("zipcode",Validator.Integer("Zipcode must be all numbers"));
A good resource to visit is mikesdotnetting.com and search for validation. He explains the use of both client side and server side very nicely. Version 2 of Webmatrix includes nice features that make use of helpers to make working with forms very easy. For
example, you can use Regular Experssions (Regex) like:
Validation.Add("zip",
Validator.Required("Zip is required"),
Validator.Regex(@"^\d{5}$",
"Enter 5 digit zip code e.g. 91234")
);
Both client and server side validation checks against your validation rules if you include the proper scripts. Again visit mikesdotnetting.com for the full explanation and use. You can have cleaner markup using helpers like so:
You will need to find a regular expression that can accommodate international postal codes, or you may need to use a custom validator that simply verifies that a specific postal code exists.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
both these scripts are in place in their own section, and because of these scripts that prompted the question as the validation being server-side or client-side? Jquery is a 3rd party library with its own identity/website for client side validation. So
I didn't know if the code in Starter Site is client-side validation via JQuery or is it server-side validation where Webmatrix adopted the Jquery library as part of its processing features.
Wow, I fumbled with RegEx( ) for over an hour and never got passed the syntax check. .... never thinking of using the @ to get by the parser and let webmatrix process the pattern.
Marked as answer by DMT20601 on Jan 02, 2013 12:42 PM
DMT20601
Member
86 Points
197 Posts
Form validation with the Webmatrix Starter Site
Dec 31, 2012 12:02 PM|LINK
I'm developing a webmatrix website using the Starter Site template. The register.cshtml demonstrates several validation features.
My question (1) is this client-side validation or server-side validation within webmatrix or both?
All the Webmatrix reading suggests client-side is nice but not dependable, therefore server-side is a must.
----------------
I need to add a zipcode to the form
(2) ... how do limit it's length to 5 numbers in the validation.
(3) and what is (line 124)
Thanks, and Happy New Year
Careed
All-Star
18764 Points
3637 Posts
Re: Form validation with the Webmatrix Starter Site
Dec 31, 2012 02:04 PM|LINK
Your validation is likely to be both client-side and server-side. Server-side is required because your server is receiving information from the Internet, thus automatically making it (your request, not the client-side validation) not dependable. The client-side validation is a bonus to prevent unnecessary posts to the server if the data is not valid to begin with.
"The oxen are slow, but the earth is patient."
DMT20601
Member
86 Points
197 Posts
Re: Form validation with the Webmatrix Starter Site
Dec 31, 2012 07:45 PM|LINK
This code works in Webmatrix to validate a forms zipcode, but not for international zipcodes.Var zipcode=""davidsa
Member
210 Points
126 Posts
Re: Form validation with the Webmatrix Starter Site
Dec 31, 2012 08:06 PM|LINK
A good resource to visit is mikesdotnetting.com and search for validation. He explains the use of both client side and server side very nicely. Version 2 of Webmatrix includes nice features that make use of helpers to make working with forms very easy. For example, you can use Regular Experssions (Regex) like:
Validation.Add("zip", Validator.Required("Zip is required"), Validator.Regex(@"^\d{5}$", "Enter 5 digit zip code e.g. 91234") );Both client and server side validation checks against your validation rules if you include the proper scripts. Again visit mikesdotnetting.com for the full explanation and use. You can have cleaner markup using helpers like so:
<div> @Html.Label("Zip Code:", "zip") @Html.TextBox("zip", Request["zip"]) @Html.ValidationMessage("zip") </div>Careed
All-Star
18764 Points
3637 Posts
Re: Form validation with the Webmatrix Starter Site
Jan 01, 2013 12:38 AM|LINK
You will need to find a regular expression that can accommodate international postal codes, or you may need to use a custom validator that simply verifies that a specific postal code exists.
"The oxen are slow, but the earth is patient."
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Form validation with the Webmatrix Starter Site
Jan 01, 2013 08:34 AM|LINK
They are server side.
Secondly if you have a look at password validation. You will get answer of second question too.
~~! FIREWALL !~~
saeed_saedva...
Member
408 Points
74 Posts
Re: Form validation with the Webmatrix Starter Site
Jan 01, 2013 09:17 AM|LINK
you need to use the scripts below too:
@section Scripts{ <script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script> <script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> }Saeed Saedvand
DMT20601
Member
86 Points
197 Posts
Re: Form validation with the Webmatrix Starter Site
Jan 02, 2013 12:34 PM|LINK
Thanks,
both these scripts are in place in their own section, and because of these scripts that prompted the question as the validation being server-side or client-side? Jquery is a 3rd party library with its own identity/website for client side validation. So I didn't know if the code in Starter Site is client-side validation via JQuery or is it server-side validation where Webmatrix adopted the Jquery library as part of its processing features.
DMT20601
Member
86 Points
197 Posts
Re: Form validation with the Webmatrix Starter Site
Jan 02, 2013 12:42 PM|LINK
Thank you,
Wow, I fumbled with RegEx( ) for over an hour and never got passed the syntax check. .... never thinking of using the @ to get by the parser and let webmatrix process the pattern.