Domanda

Regular expression in the model

 [RegularExpression("!^search for name or lastname", ErrorMessage = "Invalid search")]
 public string SearchText { get; set; }

I have a textbox with default value "search for name or lastname" and onfocus I delete the value.

But if the user clicks the search button without entering in the text in the textbox, I want the regular expression to capture it, how do I fix the regular expression to if textbox value is "search for name or lastname" then show error message. Exact match.

Thanks

È stato utile?

Soluzione

You aren't using the attribute correctly. The attribute take a regular expression and compares the input to that regular expression. If it is invalid, then the ErrorMessage is displayed.

 [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Invalid search")]
 public string SearchText { get; set; }

So something like this would only be valid if the SearchText input on your html page has letters only. Obviously, you should adjust the regex to meet your requirements.

If you want a "default value", then you may want to use the placeholder HTML attribute. http://www.w3schools.com/tags/att_input_placeholder.asp

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top