SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression help

    I'm trying to use a regular expression to determine if a variable from my form (namely a price variable) contains ONLY numbers and/or a decimal point in the format of 0.00.

    Maybe cause it's too early in the morning I'm just not getting my head around it properly.

    I have the value in a variable called $price at the moment. How would I check for this ordering and positioning in my reg exp?

    Thanks guys...

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like

    [0-9]*(\.[0-9]{2})?

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, but I need to support the entering of whole numbers OR decimals.

    but even with something like :

    if (!eregi('[0-9](\.[0-9]{2})*', $price)) {

    The user is still able to enter wierd characters or letters in certain circumstances ("50.00word" passes this expression)

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use preg_match instead of eregi, it has a more popular and powerful regular expression syntax.

    PHP Code:
    if (!preg_match('^\d+(\.\d{2})?$'$price)) { 
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh thanks, I'm relatively new to the world of PHP.

    The code you suggested is causing this error message:

    Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /home/editgallery.php on line 44

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I added some forward slashes to this and it seems to have solved that problem.

    PHP Code:
    if (!preg_match('/^[0-9]+(\.[0-9]{2})?$/'$price)) { 
    Thanks

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah right, I forgot PHP requires those silly slashes. Glad you got it working.
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    So far all languages that use the perl style regex require delimiters before and after.

    I have yet to use a language that doesn't.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •