0

I want to validate name in my react native application and I need regex for validating names

Valid names: Dr. John St. Peter's Invalid names: Dr.. John St.. Peters''s'.

Two special characters should not come side by side and only ' and . is allowed . Also name should not start with these special characters but can end with a . not with '

Below is the regex I am using but it takes any no of ' and . side by side

/[!0-9@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?]/
Paras Watts
  • 2,565
  • 4
  • 21
  • 46

3 Answers3

0

[a-zA-Z]+\.?\s?'?(?!$) Should do the trick. It works fine in JavaScript.

I haven't spent much time making it as efficient as possible, so there might be better ways to accomplish this.

Good luck.

[ EDIT ]

This should work for the most part: [a-zA-Z ]+(\.|')?[a-zA-Z ]+(\.|')?

This is what I'm testing against:
Dr. John St. Pet .er's matches: Dr. John St. and Pet .er'
Dr.'. John St.. Peter''s' matches: Dr. and John St. and Peter'
Anthony matches: Anthony
Timbo J. Fadds matches: Timbo J. Fadds
Sally Esquire matches: Sally Esquire

It only really messes up when testing against strings with lots of "illegal" characters.

Anyways, I'm not going to invest any more time into this until you clarify exactly what the input can be and what the output needs to be.

[ EDIT ]

[^(\.|')|(\.|')]+(\.|')? may work...

oldboy
  • 5,729
  • 6
  • 38
  • 86
  • I will try and let you know if this works. Anyway thanks! – Paras Watts Jun 28 '18 at 04:37
  • @ParasWatts NP! If it works, be sure to vote it up ;) – oldboy Jun 28 '18 at 04:38
  • I tried and it is not working in case '. or '' or .. are used side by side which should not be allowed – Paras Watts Jun 28 '18 at 04:42
  • why would `'.` ever be used side-by-side in a name?? also, that should have been included in your question, so people like myself don't waste our time on solutions that don't solve problems... – oldboy Jun 28 '18 at 04:48
  • Two special characters should not come side by side this I have clearly written you can check – Paras Watts Jun 28 '18 at 04:51
  • @ParasWatts ohhh, i thought u said it was doing that. my bad – oldboy Jun 28 '18 at 04:53
  • @ParasWatts i need to know can a string start with a ` `, `.`, and or `'`, and can it end with either of those characters? – oldboy Jun 28 '18 at 04:58
  • it can end with those characters but can not start, but it does not matter that much, I just want two special characters . and ' or .. or '' should not come together – Paras Watts Jun 28 '18 at 05:16
  • @ParasWatts so how do you imagine dealing with input like `Dr.'. John St.. Peter''s'` what is valid, if anything, in that string? can an output end with `.` or `'`? – oldboy Jun 28 '18 at 05:18
  • User will enter the name and if it valid then ok if not valid error will be shown, we are allowing ' and . besides alphabets and want .' or .. or '' .i.e two special characters side by side should not be allowed – Paras Watts Jun 28 '18 at 05:33
  • @ParasWatts so... `Dr.John St. Pet .er's` is a valid name in your system? lol AGAIN, you have to be more SPECIFIC – oldboy Jun 28 '18 at 05:37
  • I have updated my question.I hope it will be more clear now – Paras Watts Jun 28 '18 at 05:48
  • @ParasWatts your edit doesn't help. when i say be more specific, i mean make a list of ALL valid formats of input and ALL valid formats of output. if you can't do that, you have no idea what you're looking for............... i've edited my question nonetheless. now i'm seriously going to stop wasting my time with this until you provide the necessary information – oldboy Jun 28 '18 at 06:02
0

I advise try it and i hope be useful:

([A-z])+(.?[a-zA-Z])*('?[a-zA-Z])*

0

The regex should look something like this:

^(?!(.*(([^a-zA-Z\.\'\s])|(\.{2,})|('{2,}|\s{2,}))))

You can fork and add more test cases here:

https://regexfiddler.com/e/0ptjxdnr7y5r/test-valid-name

Rob
  • 10,851
  • 21
  • 69
  • 109