views:

20

answers:

2

I have a combo box like this

<select name="Type" id="Type" >
  <option value="0">--Select--</option>
  <option value="Open Ended">Open Ended</option>
  <option value="Property">Property</option>
</select>

and my MVC validator contains this

[Required(ErrorMessage = "Required Field")]
[RegularExpression(@"^[^0]+", ErrorMessage = "Please Select the Type")]
public string Type { get; set; }

But when I submit the form there is no error thrown even when "--Select--" is selected. Am I missing something here?

A: 

Try to specify $ sign at the end of regex.

If that will not help try to skip value attribute in "--Select--" option (and it's content too):

<option label="--SELECT--"></option>

In that case your 'Required' attribute will raise validation error (because browser will post form parameter with unspecified value or will not post this parameter at all) - it helps in my case :)

SpeCT
Sorry guys ... I had the wrong name and id in my view that caused the problem ... I changed those thing and my RegEx works fine :)
Johnson
A: 

Change this:

<option value="0">--Select--</option>

into this:

<option value="">--Select--</option>

Now the required field validation attribute is the only one you need.

AndrewDotHay