:
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.");