[RESOLVED]Validation helper for select list

Is there a particular method for validation input for select field? I used range validation marking 0 as "selection option" and then defining range from 1 onwards..

I used max limit 100 as to assuming max 100 will be added in future..

Validation.Add("region", Validator.Range( 1, 100, "Please select region!"));

<div>
  @Html.ValidationMessage("region")
  <select name="region" @Validation.For("region")>
    <option value="0">-- Select Region --</option>
     @foreach(var item in regions){
       <option @item.Id>@item.Region</option>    
     }
</select>

1) Is there a defined way or the one I am using is correct?

2) are the validation mesaages like "validationmessage and validation.for" placed correctly in the select tag.

All you need is a Required validator:

Validation.Add("region", Validator.Required("Please select a region"))

or

Validation.RequireField("region", "Please select a region");

The placement of the other two helpers is fine.

Hello Mike, as usual thankyou!

I had added validation.add method. After all learnt it from your book.Laughing

wanted to make sure that this is the right way and not a tweak.

thanks again!

The Web Pages validation system changed quite a bit between the book and Web Pages 2. Here’s an article that covers the newer version of validation: http://www.mikesdotnetting.com/article/191/validation-in-razor-web-pages-2

Leave a Reply