By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

reg exp

P: n/a
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

But it does is validated: aaa !!! aaa

What I'm doing wrong?

Thanks
Jul 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gaijinco wrote:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

But it does is validated: aaa !!! aaa

What I'm doing wrong?
In "aaa !!! aaa", /[a-z,A-Z][a-z,A-Z,0-9, ]*/ matches "aaa ", so the
expression does match the input, indeed: /[a-z,A-Z]/ matches the first `a';
/[a-z,A-Z,0-9, ]*/ matches the other two `a' and the following space character.

You need to anchor your expression so that the whole input must conform to
the format. And using the comma in there matches the comma; you should
remove it.

/^[a-z][\w ]*$/i

might be what you are looking for.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 2 '08 #2

P: n/a
Gaijinco meinte:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*
Why the colons? You don't want to match ",", do you?

/^[a-z][a-z0-9 ]*$/i will do the job.

But it does is validated: aaa !!! aaa
Your expression matches the first group of a's. That's sufficient for a
match.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur fr den alpinen Raum
Jul 2 '08 #3

P: n/a
Gregor Kofler wrote:
Gaijinco meinte:
>I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)
/^[a-z][a-z0-9 ]*$/i will do the job.
Right. \w, as I had suggested, includes the underscore ("_").
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 2 '08 #4

P: n/a
In comp.lang.javascript message <48**************@PointedEars.de>, Wed,
2 Jul 2008 18:05:46, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>
/^[a-z][\w ]*$/i

might be what you are looking for.
That will accept underlines. IIRC, I have told you about that
previously.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 2 '08 #5

P: n/a
Thomas 'PointedEars' Lahn meinte:
Gregor Kofler wrote:
>Gaijinco meinte:
>>I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*
Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)
Damn! Ah well, I was wondering what the "semi" in "semicolon" was
referring to...

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 3 '08 #6

P: n/a
Gregor Kofler wrote:
Thomas 'PointedEars' Lahn meinte:
>Gregor Kofler wrote:
>>Gaijinco meinte:
[a-z,A-Z][a-z,A-Z,0-9, ]*
Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)

Damn! Ah well, I was wondering what the "semi" in "semicolon" was
referring to...
And it becomes even more interesting when you consider where "colon"
comes from. (I have always found etymology to be a quite fascinating subject.)

<http://en.wikipedia.org/wiki/Colon_(punctuation)#History>
Regards,

PointedEars, F'up2p
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 3 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.