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
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.
Link to comment
Share on other sites

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