PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Regular expression for validating a number Posted by ITA on 31 Dec 2004 at 8:31 AM
This message was edited by ITA at 2004-12-31 8:43:19

I want to write a regular expression which only allows the user to enter a number, and only a number no text or other chars, into a textbox in a form. The regular expression should not allow spaces to be entered.

I've come up with this:

^[0-9]

Is there anything wrong with that? - it seems to work.

Can anyone come up with anything or fix any error I may have made? Please include the reg expression in your reply.

Many thanks.

ITA
Report
Re: Regular expression for validating a number Posted by Jonathan on 31 Dec 2004 at 9:50 AM
: This message was edited by ITA at 2004-12-31 8:43:19

: I want to write a regular expression which only allows the user to enter a number, and only a number no text or other chars, into a textbox in a form. The regular expression should not allow spaces to be entered.
:
: I've come up with this:
:
:
: ^[0-9]
: 

: Is there anything wrong with that? - it seems to work.
:
What do you mean by "number"? Your regex checks if the first character is a digit, and that's it. So the string "1abc" would pass. If you want to check if the string just contains one digit, then you need to match the end of the string to, using $.

^[0-9]$

If you want to allow more than one digit, e.g. so 1234 is valid, use this:-

^[0-9]+$

Are you using POSIX Extended regexes (ereg*) or PCRE (preg*)? If the second, [0-9] can just be written as \d, so you get:-

/^\d+$/

(PCRE requires /'s around the pattern match. Or some other seperator.)

If you want to optionally allow a decimal point in there, use:-

/^\d+(\.\d+)?$/

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Report
Re: Regular expression for validating a number Posted by ITA on 1 Jan 2005 at 7:39 AM

: What do you mean by "number"?

I mean a whole number eg 5 or 10 or 245 (no decimals and no other characters like spaces).


: If you want to allow more than one digit, e.g. so 1234 is valid, use this:-

: ^[0-9]+$

I think the above is what I am looking for so long as the above doesn't allow other chars. Does it?


: Are you using POSIX Extended regexes (ereg*) or PCRE (preg*)?

I'm using ereg() but I'm quite happy to use preg() if it's easier for you (perl guru).

:
: (PCRE requires /'s around the pattern match. Or some other seperator.)
:
: If you want to optionally allow a decimal point in there, use:-
:
: /^\d+(\.\d+)?$/

I don't want to allow a decimal point - I want to allow whole numbers only.


Thanks for the reply. This is the first time I've tried to do a regular expression - as you can see I'm quite hopeless at it. Unfortunately the project which I'm working on has a tight deadline so it's difficult to find time to learn how to use them properly (I will get round to it!)

Many thanks again.

ITA

Report
Re: Regular expression for validating a number Posted by Jonathan on 2 Jan 2005 at 8:40 AM
:
: : What do you mean by "number"?
:
: I mean a whole number eg 5 or 10 or 245 (no decimals and no other
: characters like spaces).
:
:
: : If you want to allow more than one digit, e.g. so 1234 is valid, use this:-
:
: : ^[0-9]+$
:
: I think the above is what I am looking for so long as the above
: doesn't allow other chars. Does it?
:
Yes, the above is what you are looking for. That pattern will only match if the entire string represents a whole number. No other characters are allowed, spaces included.

: : Are you using POSIX Extended regexes (ereg*) or PCRE (preg*)?
:
: I'm using ereg() but I'm quite happy to use preg() if it's easier for
: you (perl guru).
:
ereg() is fine for this, but be sure to use [0-9] with ereg, and not \d which only works in preg().

: Thanks for the reply. This is the first time I've tried to do a
: regular expression - as you can see I'm quite hopeless at it.
: Unfortunately the project which I'm working on has a tight deadline so
: it's difficult to find time to learn how to use them properly (I will
: get round to it!)
:
They take some time to get used to, but are very powerful and useful once you have learnt them. Don't feel bad about not getting them right away - I sure didn't.

Jonathan

###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.