简体   繁体   English

用于验证名称的正则表达式

[英]Regular expression for validating name

I am trying to validate an address input field on my form and it must be entered in the following format "City, Province, Country" 我正在尝试验证表单上的地址输入字段,并且必须以以下格式输入“城市,省,国家/地区”

I am using the following regular expression to do this 我正在使用以下正则表达式来做到这一点

^[^,]+,\s\w[^,]+,\s\w[^,]+$

When entering "New York, New York, United States" this would be valid. 当输入“纽约,美国纽约”时,这将是有效的。 However, when I enter "N, N, United States" it does not work. 但是,当我输入“ N,N,美国”时,它不起作用。 It seems one letter words will not work. 似乎一个字母的单词是行不通的。 I need to allow users to only enter a single letter as well. 我还需要允许用户仅输入一个字母。

Not sure if the regular expression I have written is complicated and can be simplified 不知道我写的正则表达式是否复杂并且可以简化

Make sure to check for dashes: 确保检查破折号:

((\\w)|(-)|(\\s))+,\\s*((\\w)|(-)|(\\s))+,\\s*((\\w)|(-)|(\\s))+$ ((\\ w)|(-)|(\\ s))+,\\ s *((\\ w)|(-)|(\\ s))+,\\ s *((\\ w)|(-)| (\\ s))+ $

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM