Ninjakreborn Posted November 1, 2006 Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted November 1, 2006 Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 1, 2006 Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.