Jump to content

preg_match to check format of a price


RLJ

Recommended Posts

Hi, I want to use preg_match in a very simple line of code to check whether a price someone enters in a form is in the correct format.

 

What I want to do is:

 

1) convert "," to "." so that if someone enters a price in european format: 5,46 it gets changed to 5.46

2) convert e.g. 89 to 89.00 and 5.5 to 5.50

3) allow only numbers "0-9" and one fullstop "." or one comma "," (which gets changed to a fullstop)

4) has to be between 1-5 digits

5) has to be at least one number before the "." or ","

 

my PHP code is as follows:

 

if (preg_match($checkpattern,$price)==0) {echo "Price error"; $verify="bad";}

(and I guess a preg_match_replace line to achieve the first 2 requirements)

 

Could someone please help me with what $checkpattern I need to use in preg_match(_replace) to achieve the above? I've been struggling for ages but can't seem to get it quite right.

 

Thanks!

Link to comment
Share on other sites

Need some clarification on a few of the rules..

 

1) Do you mean replace commas between say pounds and pence (£1.99) or between large numbers (£1,999.99)?

 

3) What about numbers like £1,999.99?

 

4) What about numbers like 5.5, does the padded 0 count as a digit?

Link to comment
Share on other sites

ad. 1) I only mean to replace the comma between say pounds and pence, the particular application I have in mind for this price input form would mean users (hopefully) wouldn't enter a number larger than say 999 and so wouldn't use the comma for a large number like this 1,234.50, but I suppose it might be a good idea to also include a "no numbers larger than 999" rule.

 

ad. 3) idem

 

ad. 4) actually ignore (4), I can set up the html input box so that only 5 digits can be entered.

 

Thanks in advance

Link to comment
Share on other sites

Your original question states you wish to check whether an input is in the right format, yet steps one and two are conversions, which has nothing to do with pattern matching. Steps one and two cannot be achieved using preg_match, and will need to be done first. Once you have done the conversion your pattern would be as simple as...

 

$pattern = '#[0-9]{1,3}\.[0-9]{2}#';

Link to comment
Share on other sites

Thanks cags.

 

That's why I mentioned perhaps a "preg_replace" for the 1st 2 steps. But am I right in thinking I would still need a pattern for this? If so, what pattern should I use? I guess step 1 is easy enough, but I have no idea how to approach step 2. (sorry, I'm new to this stuff)

Link to comment
Share on other sites

OK this what I have so far:

 

if(isset($_REQUEST['price'])) 
{

$price = $_REQUEST['price'];
$price=preg_replace("#[,]#",".",$price);   //replaces "," with "."
$price=preg_replace("#(^[0-9]{1,3}$)#","$1.00",$price);   //replaces 5 with 5.00 or 34 with 34.00, etc.
$price=preg_replace("#(^[0-9]{1,3})(\.[0-9]{1}$)#","$1$2 0",$price);   //replaces 2.3 with 2.30 or 23.5 with 23.50 etc.

$pricecheckpattern	= "#^[0-9]{1,3}\.[0-9]{2}$#";
if (preg_match($pricecheckpattern,$price)==0) {echo "Price error"; $verify="bad";}  
} 

 

This works, except it replaces "2.3" with "2.3 0" instead of "2.30". This is because I can't tag on the zero straight after $2 because then it reads $20, which doesn't exist. Does anyone know a solution for this?

 

Also, if anyone can see how to reduce the 3 preg_replace lines to just a single line with one pattern, please let me know.

 

Thanks!

Link to comment
Share on other sites

a.) I've never seen anyone use a comma to delimit the 'pence' section of a currency, perhaps some people do, but this is easily fixed with str_replace.

b.) Making sure it's two decimal places is probably best done with number_format. I don't see any real need for regular expressions at all.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.