Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:30:41+00:00 2026-06-08T17:30:41+00:00

I am currently validating US Currency along with a decimal number format. I am

  • 0

I am currently validating US Currency along with a decimal number format. I am using the function money_formatbut the issues is that certain values is returning an error.

How can i have the function work when the input value is 0.00 or 0.50? Example

PHP

if (isset($_POST['price'])){
    $price = $_POST['price'];
    $priceString = empty($price['price'])?null:trim($price['price']);
        if(!empty($priceString)) { 
            $price = str_replace("$", "", $price); 
                if  (preg_match('/^[+\-]?\d+(\.\d+)?$/', $price)){
                 echo ('<div id="price"><span id="resultval"><h2>Price:</h2>'.money_format('%n', $price).'</span></div>');
                           }
                     else {
             echo ('<div id="price"><span id="resultval"><h2>Price:</h2><div class="errorMessage">Enter a valid number in US currency format.</div></span></div>'); 
                           }
}
            else {
            echo '';
            }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:30:44+00:00Added an answer on June 8, 2026 at 5:30 pm

    The error is here:

    $price = $_POST['price'];
    $priceString = empty($price['price'])?null:trim($price['price']);
    

    Note that that $price is a String; so, $price['price'] returns the first character of that array because the interpreter should logically attempt $price[intval('price')] where intval('price') == 0. Consequently, $price['price'] == $price[0].

    For example, if the input was 0.00, then $price['price'] would return 0 which is considered empty according to the PHP manual.

    I think you intend for the following (which works with 0.00 and 0.50):

    $price = $_POST['price'];
    $priceString = empty($price)?null:trim($price);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i am validating the validation form for phone number using the following code
I am working currently with PHP validation of decimal values. The function works well
I have a application that needs to accept float or currency values in edit
i have 3 form input text for phone number. currently i am validating each
I currently have a form that requests the user's email, cell phone number, and
I have a page with a number of fields on it that I'm using
I'm currently using the validation code listed here in an application. I'd like to
I am currently using $.blur(fn();) for form validation, but this only takes effect when
I'm going through my code to replace any instances I'm using the ereg() function
Where should I locate the code for validating an employee ID (badge) that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.