开发者

JS Validation Allow Space

开发者 https://www.devze.com 2023-02-28 00:51 出处:网络
I have regExp in JS to validate first/last names var reg开发者_如何转开发Exp = /^[A-Za-z]$/;, and I want to allow space with it, how can i?

I have regExp in JS to validate first/last names var reg开发者_如何转开发Exp = /^[A-Za-z]$/;, and I want to allow space with it, how can i?

Right now its allowing a to z and A to Z chars only, i only need to allow space.

Thanks in advance.


The solution by @ThiefMaster shows how you can add a space to your regular expression.

However, if your intent is to match a name then you can probably do better. For example, you could say that a valid name is any one or more valid unicode letter characters (adapted from XRegExp):

var unicodeWord = XRegExp("\\p{L}+");
unicodeWord.test("San Juan"); // true
unicodeWord.test("Русский"); // true
unicodeWord.test("日本語"); // true
unicodeWord.test("العربية"); // true

[Edit]

Note that the above expression matches any name which has one or more Unicode letter including names which include other characters (like "San Juan", "O'Connors", or "Anne-Marie").


You cannot validate natural names by any regular expression. So please stop doing it - it just pisses of users who have characters in their name not matchign your regex.

Anyway, /^[A-Za-z ]$/ is the regex you are looking for.

0

精彩评论

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