开发者

How to modify this JavaScript regex to check for a person's valid name?

开发者 https://www.devze.com 2023-01-29 06:53 出处:网络
This JavaScript regex checks to see if a person has entered a valid username: var regexp = \"/^[a-z]([0-9a-z])+$/i\";

This JavaScript regex checks to see if a person has entered a valid username:

var regexp = "/^[a-z]([0-9a-z])+$/i";
if(!regexp.test(name)) 
  return "Name may consist of letter, numbers and start with a letter." );

But I want to check whether the person has entered a valid name.

In other words:

Cher
Michael Jackson
Mary J. Blige
François Truffaut

I can't think of any cases where anything other than lett开发者_StackOverflow社区ers and maybe a period would be valid in a person's name.

I want to reject any other types of punctuation as a validation mistake.

But I want to make sure that, for example, François Truffaut would not be rejected because he has that funny version of the letter c that the French use.

How would I do this in a Javascript regex


Why not try to disallow just the punctuation you don't want in the name, probably a shorter list.

var regexp = "/^[\\\/$&^!@#%*~\?\[\]=\_\(\)]/"

That may not be a complete list but it should be a short list. These were the ones that seemed, the most reasonable IMO.


Try [\d\w.]+. It should take care of those funny French characters. Test it here online (it's for ruby, but it's almost the same).

[\d\w.][\d\w. ]*[\d\w.] would take the full name, including possible spaces in the middle. Limits the name length to two though. But it should be ok.


Enforcing character restraints on a name is a bad idea.

0

精彩评论

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