regex in PHP to enter user weight

Posted on 2009-12-30
Last Modified: 2013-12-12
I have a from where users enter their weight.  The form is posted to a page (code below) which validates the entered weight and updates the database.

It is the validation (using preg_match) that I am having trouble with.  Examples of acceptable format:


In english, a number between 10 and 999, with or without one decimal point.

If the format is acceptable, the page should process the form (ie. update the database).  With the current code, the form is processed no matter what is entered.  I have tried simplifying the regex, eg. (preg_match("/^\d)$/", $weight)); but the form will be processed if I enter a letter or number.

// database connection and other facebook code above

// Greet the currently logged-in user!

echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";



  die('No post.');


$weight = (string) $_POST['weight'];

if (empty($weight))


  die('You did not enter anything. Please go back.');


$matched = (preg_match("/^[1-9]\d{1,2}((\.\d){0,1})$/", $weight));

if ($matched = 0)


  die('Your weight does not follow the

required format (explain correct format...). Please try again.');


echo sprintf('Thank you!', $user);

//  This php script catches a posted form and writes the result and userid of whoever posted it

      if($_POST['add'] == "true")


          $weight = $_POST['weight'];

$date=date("Y-m-d") ;       

mysql_query("INSERT INTO weight (bookfaceuserid, weight, date) VALUES('$user', '$weight', '$date')");





Open in new window

Question by:willow87
    LVL 82

    Accepted Solution

    if ($matched == 0)//TWO equal signs needed

    Author Closing Comment

    Ah!  I was so close.  Showing what a beginner I am by missing that one. Works perfectly now - many thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now