469,315 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

regular expression question

Hi,
I'd like to use regular expression for checking validity of a field.
What would be the expression for :

firstname, lastname, organisation23

so basically, a string + comma then another string + comma then a
alphanumeric string
thanks

Aug 14 '06 #1
10 1443
Rik
sa*************@googlemail.com wrote:
Hi,
I'd like to use regular expression for checking validity of a field.
What would be the expression for :

firstname, lastname, organisation23

so basically, a string + comma then another string + comma then a
alphanumeric string
Basically:
^([a-z]+,\s*){2}[a-z0-9]+$/i

Problem here is exotic characters like , , etc, which are offcourse
totally valid in names, but won't match here. Also spaces (\s), -, ' etc....

Grtz,
--
Rik Wasmus
Aug 14 '06 #2
Thanks Rik,

I don't need to handle those special characters anyway ... lucky me.
however i do need to handle spaces ! I mean spaces should be allowed.

Aug 14 '06 #3
Rik
sa*************@googlemail.com wrote:
Thanks Rik,

I don't need to handle those special characters anyway ... lucky me.
however i do need to handle spaces ! I mean spaces should be allowed.
^([a-z\s]+,){2}[a-z0-9\s]+$/i
Grtz,
--
Rik Wasmus
Aug 14 '06 #4
Thanks,

I've tried to use it this way :

/^([a-z\s]+,){2}[a-z0-9\s]+$/i.exec(myString)

but whatever the value of myString is, it always return null. Any clue
?

Aug 14 '06 #5
Please ignore my last post.

Your regex, works just great !
thank you very much.

Aug 14 '06 #6
After a few tests I can say it works almost well.
The only thing is that it allows me to enter something like
test, ,test

so basically, spaces should be allowed BUT there should be at least one
character, in other words empty strings should not be allowed.

Aug 14 '06 #7
Rik
sa*************@googlemail.com wrote:
After a few tests I can say it works almost well.
The only thing is that it allows me to enter something like
test, ,test

so basically, spaces should be allowed BUT there should be at least
one character, in other words empty strings should not be allowed.

Tsssk, get your requirements straight :-).

Here you go:
^(\s*([a-z]+\s*)+,){2}\s*([a-z0-9]+\s*)+$/i

Grtz,
--
Rik Wasmus
Aug 14 '06 #8
JRS: In article <11**********************@75g2000cwc.googlegroups. com>,
dated Mon, 14 Aug 2006 06:19:25 remote, seen in
news:comp.lang.javascript, sa*************@googlemail.com posted :
>I'd like to use regular expression for checking validity of a field.
What would be the expression for :

firstname, lastname, organisation23

so basically, a string + comma then another string + comma then a
alphanumeric string
Maybe no : better a commaless string + comma then another commaless
string + comma then a string; and ignoring whitespace between elements
if you use, say, .match to get the three elements.

Only of it is impossible for the organisation name to contain a comma
(or other non-alphanumeric) would your description serve,

Consider the case of

St = "James, Smith, Mart-Wal & sons, Inc."
M = St.match(/^([^,]+),\s*([^,]+),\s*(.+$)/)

No match : M = null
Match : M[1]..M[3] are the fields.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
Aug 14 '06 #9
John,
Your comment is very relevant. I had not thought of that :(
How could I valid non-alphanumeric characters in the third string ?

Thank you

Aug 15 '06 #10
Rik
sa*************@googlemail.com wrote:
John,
Your comment is very relevant. I had not thought of that :(
How could I valid non-alphanumeric characters in the third string ?

Thank you
^(\s*([a-z]+\s*)+,){2}\s*([a-z0-9]+.*)+$/i

Grtz,
--
Rik Wasmus
Aug 15 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bradley Plett | last post: by
10 posts views Thread by Lee Kuhn | last post: by
18 posts views Thread by Q. John Chen | last post: by
7 posts views Thread by norton | last post: by
7 posts views Thread by Billa | last post: by
6 posts views Thread by Ludwig | last post: by
3 posts views Thread by Zach | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.