开发者

Regex for 'Title', selected value should start with 'M'

Im using c# and I need to be able to check that the user sel开发者_StackOverflow中文版ects a title from the dropdown list. How would I set the regex to check that the selection starts with M, the values in my dropdown list are Mr, Ms and Mrs. This regex would be used in my customvalidator in Visual Studio 2008 3.5 framework.

Edit: Let me rephrase it, What do i type in my ValidationExpression field for my RegularExpressionValidator Validation type.

[1]: http://imageshack.us/photo/my-images/836/imglv.jpg "


To answer your question:

Regex.IsMatch( inputString, "^M" )

That said, it really doesn't sound like a job for regular expressions. You could use StartsWith, which is more sane, but what happens when "Mike Smith" registers, and doesn't select a title?

If you want to ensure that a user selected your specific value, then validating the selected value is really what you should do here. Feel free to post more information if that scenario isn't possible.

On a side note, I can't resist posting this quote:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Edit: I missed your edit before I posted. You would be better off using a RequiredFieldValidator and setting InitialValue to "Select A Title".


Use StartsWith method, e.g.: s.StartsWith("M").

Or using regex:

Regex.IsMatch(input, @"^M")


you can use startWith :

yourString.StartsWith("M");

it has also an overload to specify the type of comparison

for more info:

http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜