Regular expression for validating a number

[b][red]This message was edited by ITA at 2004-12-31 8:43:19[/red][/b][hr]
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:

[code]
^[0-9]
[/code]
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

Comments

  • : [b][red]This message was edited by ITA at 2004-12-31 8:43:19[/red][/b][hr]
    : 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:
    :
    : [code]
    : ^[0-9]
    : [/code]
    : 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.");


  • : 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

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

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories