Jump to content

made quick expression, not working.


Ninjakreborn

Recommended Posts

I was wondering why this wasn't working
[code]<?php
$expression = "^[0-9].[0-9]$";
if (!ereg($expression, $_POST['price1'])) { // if it is check it's format with regex
  $errorhandler .= "Price 1 has to be formatted like 0.00 and it is not.<br />";
}
?>[/code]
The regular expression was suppose to check for 0-9 at the beginning.  THen look for a "." then another number set between 0-9 at the end.
Because ^ means at the beginning and $ means at the end, but it's not working, it's returning false even when it'se suppose to return true.
Link to comment
https://forums.phpfreaks.com/topic/25829-made-quick-expression-not-working/
Share on other sites

try:
[code]$expression = '^[0-9]\.[0-9]$'[/code]
That will look for a literal period(.)  It should match numbers like 5.6, 6.7, 7.8 etc., but not 56, 56.7, 5.67, etc.

You can also consider a subexpression like:
[code]$expression = '([0-9]\.[0-9])'[/code]
and that should match the same thing without the beginning and ending requirements.
One other note, that still will not match your stated "x.xx" format. You're in essence matching for a "x.x" format. You've got to tell the regex engine to match two decimal places after the dot:
[code]
<?php
$expression = '|^[\d]{1}\.[\d]{2}$|';
?>
[/code]

That specifically searches for [b]one[/b] digit followed by a decimal followed by [b]two[/b] digits.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.