SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Please help with this regular expression preg_match()

    Is there anyway to force a specific string format?
    For example this requires numbers and no leading zeros.
    Code:
    ^[1-9][0-9]+$
    What I'm looking to do is force the end-user to enter the string 'lbs' (in that exact order) after the numbers so they'd be forced to enter say '125lbs'.
    Also for height, would it be possible to force the ' (hyphen) and " (quotes) in a format like 5'11"?
    Any help will greatly be appreciated.

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it.
    Code:
    ^[0-9]'[0-9]{1,2}"$
    Outside of []'s, characters act as literals, so you just put them where they need to be in the string.

    For your pound issue, use
    Code:
    ^[1-9]{1}[0-9]*lbs$

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zarin Denatrose View Post
    This should do it.
    Code:
    ^[0-9]'[0-9]{1,2}"$
    Outside of []'s, characters act as literals, so you just put them where they need to be in the string.

    For your pound issue, use
    Code:
    ^[1-9]{1}[0-9]*lbs$
    What's difference between using
    [1-9]{1} & [0-9] ?

  4. #4
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first doesn't really NEED the {1}, but the difference between the two is that the first range excludes 0, making leading zeroes fail the match.

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zarin Denatrose View Post
    The first doesn't really NEED the {1}, but the difference between the two is that the first range excludes 0, making leading zeroes fail the match.
    Sorry my mistake.
    I was mean to ask difference between two rather:
    [1-9]{1} & [1-9]

  6. #6
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah. Yes, there really isn't one.

  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zarin Denatrose View Post
    Ah. Yes, there really isn't one.
    expecting the same


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
  •