Please help with regular expression

P

paulsmith5

Hi,

I'm developing a web app and I want to validate a users input on a
form. I need a regular expression to validate a string which must be
from 1 to 5 characters in length - each character must be a number from
0 to 9 however the string can't be comprised entirely of 0's (i.e.
invalid entries would be 0, 00, 000, 0000, and 00000).

Hope someone can help.

Thanks,

Paul
 
L

loyal_caper

I'm no regex expert, but this seems to work:

(?=^[0-9]{1,5}$)[0]*[1-9][0-9]*

Walter
----
Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
 
L

loyal_caper

a bit simpler:

(?=^[0-9]{1,5}$)0*[1-9]

Walter

----
Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
 
J

Julian Turner

I'm developing a web app and I want to validate a users input on a
form. I need a regular expression to validate a string which must be
from 1 to 5 characters in length - each character must be a number from
0 to 9 however the string can't be comprised entirely of 0's (i.e.
invalid entries would be 0, 00, 000, 0000, and 00000).
[/snip]

Another alternative:-

var r=/^(?!0+$)[0-9]{1,5}$/;

However, I think some older browsers (e.g. some older versions of IE)
do not support look aheads ?: ?= and ?!

Regards

Julian
 
R

RobG

Hi,

I'm developing a web app and I want to validate a users input on a
form. I need a regular expression to validate a string which must be
from 1 to 5 characters in length - each character must be a number from
0 to 9 however the string can't be comprised entirely of 0's (i.e.
invalid entries would be 0, 00, 000, 0000, and 00000).

Hope someone can help.

You've been given some great responses, here's one for browsers that
don't support look-ahead:

if (/^\d{1,5}$/.test(num) && num.replace(/0/g,'').length){
alert('OK');
} else {
alert('bzzzzt');
}


Where num is the string entered by the user.
 
D

Dr John Stockton

JRS: In article <43f5cb55$0$17107$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Fri, 17 Feb 2006 23:10:03 remote, seen in
news:comp.lang.javascript said:
if (/^\d{1,5}$/.test(num) && num.replace(/0/g,'').length){

or ?

if (/^\d{1,5}$/.test(num) && /[1-9]/.test(num)) ) {
 
P

paulsmith5

Hi Walter,

Thanks for the reply. You're an expert in my books - it works
brilliantly.
 
P

paulsmith5

Hi Rob,

Yes the responses have been great - thanks for the tip to overcome the
look-ahead thingy
 
R

Randy Webb

(e-mail address removed) said the following on 2/20/2006 7:19 AM:
Hi Rob,

Yes the responses have been great - thanks for the tip to overcome the
look-ahead thingy

How about another "tip" then?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top