regular expression validator, get rid of unwanted characters

S

Steve

Hi I have a regular expression validator for my username field which
just allows 3 to 30 characters without spaces to be inputted by the
user.

^\S{3,30}$

How can I add to this expression not to allow characters such as
"?/{]-.*'!" etc...?

Thank you.
 
L

Lucas Tam

Hi I have a regular expression validator for my username field which
just allows 3 to 30 characters without spaces to be inputted by the
user.

^\S{3,30}$

How can I add to this expression not to allow characters such as
"?/{]-.*'!" etc...?


^[A-Za-z]{3,30}$

That should do it
 
S

Steve

Thank you Lucas for the help. I really want to learn how I can specify
which characters I do NOT want as opposed to what I want. Do you know
the syntax for this?
 
S

S. Justin Gengo

Steve,

Here's an excerpt from:

http://www.15seconds.com/issue/010301.htm (Where there are some great
regular expression examples given.)

A caret (^) at the begging of the pattern string enforces that the target
string should start with the pattern string. But if you put the caret inside
the square brackets [^], then it is altogether a different meaning. Caret
inside the square bracket means "don't match the characters inside the
brackets."
m[^ai]n --- matches men, mend, diamond

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
L

Lucas Tam

Thank you Lucas for the help. I really want to learn how I can specify
which characters I do NOT want as opposed to what I want. Do you know
the syntax for this?

Just be aware if you go that route, you'll have to filter out a lot of
characters (i.e. numeric, non-ascii, symbols, etc). Basically there are a
lot of combinations you'll need to filter. If you insist on going this
route, you might want to look at Unicode Character sets. .NET's regular
expression parser has the ability to specific sets of characters with
shortcuts:

\p{name} Matches any character in the named character class specified by
{name}. Supported names are Unicode groups and block ranges. For example,
Ll, Nd, Z, IsGreek, IsBoxDrawing.

\P{name} Matches text not included in groups and block ranges specified in
{name}.

It might be best to stick with the KISS principle, and only allow a certain
character set : )
 
S

Steve

I think you are right. I did actually want to allow a unicode
characterset to be honest as you guessed. i.e. i want to allow
alphanumeric+ a unicode character set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top