volume one
volume one

Reputation: 7563

Regex to check existence of numbers and special characters only

I'm trying to allow users to register a username, as long as it doesn't have any special characters (e.g. $£*%&) or numbers in it.

I have made the following which works, but it won't allow for non-English characters. That is, if I put in some text like विकिपी then it matches as not being an alphabetical character which is not what I want.

[^a-zA-Z0-9'-]+$

I want to allow users to type in their username in whatever alphabet they want. So they could use the English alphabet or Hindi or Chinese etc. It should only check the string for the existence of numbers and non-alphabetic characters.

Is there some kind of regex range to match for special characters?

Clarification: I need to check the username (string) entered into a form for any numbers and non-alphabet characters in order to flag it as invalid. So the regex needs to look at the string and if it matches a number or special-character then the server will throw an error. By flagging them as errors, I'm telling the user to only use characters in the alphabet of their chosen language.

Upvotes: 1

Views: 397

Answers (1)

anubhava
anubhava

Reputation: 785186

In Coldfusion(Java) you can check input using unicode property as well:

\\P{IsAlphabetic}

\\P{IsAlphabetic} matches any unicode non-letter.

Examples:

boolean a = "विकिपीडिया".matches(".*?\\P{IsAlphabetic}.*"); // false
boolean b = "विकिपीडिया7".matches(".*?\\P{IsAlphabetic}.*"); // true
boolean c = "-विकिपीडिया".matches(".*?\\P{IsAlphabetic}.*"); // true

Upvotes: 3

Related Questions