Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,414
    Thanks
    68
    Thanked 102 Times in 101 Posts

    Checking textbox for correct format

    I have a standard textbox with a default value of 0.00 the customer needs to enter a value that is in the same format.

    I am guessing preg_match would be the right code to use? I need to allow up to 7 digits and only two ending digits. So basically 0.01 to 9999999.99
    But need to block if it's like 50000 or 500,00 etc. I could make two different textboxes, but I want them to be the same field in the database if possible.

    Thanks!

    PHP Code:
    <b>Price:</b></td><td><input type="text" name="price" size="6" maxlength="9" value="0.00"
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    I think the REGEX you'll want is this: ^[0-9]{1,7}[.][0-9]{2}$

    This will allow 1 to 7 digits up front, a decimal point, and then two digits after the decimal.

    In PHP:

    PHP Code:
    if( !preg_match'/^[0-9]{1,7}[.][0-9]{2}$/'$myPriceField ) )
         echo( 
    "Whoops! Someone didn't follow the format." ); 
    I would, however, for user clarity purposes, add some JavaScript to the client side to help the user format better (i.e: as they're typing, do some auto format if possible).
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com


  •  

    Posting Permissions

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