Question

I'm trying to make a name field filter with jQuery and I tried so many regular expressions and no of them work fully.

All I need is to make the name field as the name field - nothing else, e.g. Adam Jeremy Smith for example. So there would be no possibility to type $ or / or _ or ~ or 0-9 or anything else? How to do this?

I thought it should be:

/[^\\p{L}]+([^0-9])+$/

Yeah it works only if you type e.g. 'Adam Je4' but if you continue and write 'Adam Je4remy' it doesn't work and validates it incorrectly?

How to achieve it?

Was it helpful?

Solution

If you define name as something that consists of 1 or more words, separated by spaces, where each word consists of one or more letters, and starts with uppercase, it's written something like...

/^[A-Z][a-z]+(\s+[A-Z][a-z]+)*$/

But it's actually much more complicated, see - and it's not only about letters (\p{L} in your example; btw, property unicode classes are not supported by JS, you have to use plugins (like this one, for example) to implement such functionality).

For example, will you allow apostrophes in your names? If so, how can they be used (probably no name has two apostrophes close, like this - I''van Ivanov? What about hyphens? What about different capitalization rules: will you consider brian d foy a true name, for example? And so on...

The point is you have to balance what you actually expect to see with the complexity of your regex.

OTHER TIPS

Strictly limiting the format of names like that is bad practice, because it fails to take into account all sorts of cases - people with apostrophes and dashes in their names, people from European/Asian countries who use additional characters, and so on.

See this article for more info: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

The answer is: /^[A-z\s]+$/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top