0

Is there a way where I can find if any of these strings are entered. I want to see if the user entered "Mr", "Miss", "Mrs" or "Ms".

I have tried /^(Miss|Mr|Mrs|Ms)$/ but to no avail.

The context is that I have a text box that users should enter their title into.

I want users to only Enter "Mr", "Miss", "Mrs", and "Ms". If they enter anything else, throw an error.

jscs
  • 63,694
  • 13
  • 151
  • 195
helloGoodDay
  • 69
  • 2
  • 9
  • 2
    Dr, Rev, Sir, Lord, Dame, Lady... This may be one of those questions where a little more context could help – Liath Sep 09 '14 at 07:36
  • 1
    Works for me... http://rubular.com/r/WO7fLbcTMW – deceze Sep 09 '14 at 07:36
  • Here's a great tool for testing regexes - http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx – Liath Sep 09 '14 at 07:36
  • I'm using a RegularExpressionValidator in ASP.net to check whether users entered their title right. If Miss/Mr/Mrs/Ms was not entered then throw an error. UMM is there a way to do an inverse of that regex I did? – helloGoodDay Sep 09 '14 at 07:56
  • 2
    Use a drop down menu instead of a text box. – jscs Sep 09 '14 at 08:45
  • @JoshCaswell hey Josh, thanks for replying. The thing is I get a horrendous problem that I couldn't solve, even with looking at lots of solutions if I implement a drop down list So I've reverted to a textbox – helloGoodDay Sep 09 '14 at 09:13
  • Did you try [without the delimiters](http://stackoverflow.com/a/13626639/3110638) `/` ? – Jonny 5 Sep 09 '14 at 10:34
  • That's like saying that your lawnmower won't start, so you're cutting the grass with a pair of scissors. Fix your lawnmower. This is going to be a _terrible_ user experience -- reading your mind to figure out _exactly_ which honorifics you planned for and _exactly_ how you spelled them (no period/full stop, e.g., which should be present in American English but perhaps not other dialects). – jscs Sep 09 '14 at 18:57
  • @Jonny5 You are a legend. Thank you so much (Y) I wish i can give you reddit gold – helloGoodDay Sep 10 '14 at 03:18
  • @JoshCaswell I agree, i've been searching for the answer for 2 days and I just can't seem to get it. I really regret doing this method, but it's the only way that works. – helloGoodDay Sep 10 '14 at 03:19
  • You could probably ask about your problems with the combo box here too. – jscs Sep 10 '14 at 03:45
  • @JoshCaswell Here it is :) http://stackoverflow.com/questions/25718960/detailsusertitle-has-a-selectedvalue-which-is-invalid-because-it-does-not-exis – helloGoodDay Sep 10 '14 at 04:28
  • If you limit the title like that, you'll get complaints from customers. People with titles tend to be picky about them. It's almost like they have a sense of... [puts on sunglasses] ...entitlement. – Matt Gibson Sep 10 '14 at 09:40
  • @MattGibson Oh so true oh so true – helloGoodDay Sep 10 '14 at 10:08

4 Answers4

2

Maybe all you need to do is to get rid of the $ at the end and replace it with a \b so that if I enter 'Mr John' you can recognize it.

Example link: http://regexr.com/39faa

mpcabd
  • 1,813
  • 15
  • 20
1

A more compact version of your regex might be:

/^(M(?:is)?s|(?:Mrs?))/

Have look at this demo.

As mentioned in another answer's comment, if the search string can lie in the middle of the string as well then remove ^ from pattern.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
1
/^(Mr|Mrs|Mr|Dr|Er)\.[A-Za-z]+$/;

Here ^(Mr|Mrs|Mr|Dr|Er) will check for the starting of the string.

\. will allow a dot after Title

And then there can be any word or letter so array of words.

Gryu
  • 2,102
  • 2
  • 16
  • 29
0

Try this

\bM(?:rs?|s|iss)\b

Demo

walid toumi
  • 2,172
  • 1
  • 13
  • 10