FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


regular expression





hbrooks
I an after a regular expression that will only allow numbers in a certain field. I can create one that will allow whole numbers or decimals but won't accept an number like .54
Can anyone help
Thanks
hexkid
hbrooks wrote:
I an after a regular expression that will only allow numbers in a certain field.
Will you go for two regexps?

Code:
<?php
$rx1 = '/\d/';
$rx2 = '/^[+-]?\d*(?:\.\d+)?$/';

$CERTAIN_FIELDs = array('', 'forty-two', '42', '0.8', '8.0', '8.', '.8', '+.8', '-.8', '-', '4.0.2', '4+2');
foreach ($CERTAIN_FIELDs as $CERTAIN_FIELD) {
  echo '<tt style="background-color: yellow">* ', $CERTAIN_FIELD, ' *</tt> ';
  if (preg_match($rx1, $CERTAIN_FIELD) && preg_match($rx2, $CERTAIN_FIELD)) {
    echo 'is ';
  } else {
    echo 'is not ';
  }
  echo "good.<br>\n";
}
?>
hbrooks
Thanks.
Works great
Related topics
Formulate Regular Expression
Help with regular expression
[JS] Regular expressions global match get first group
Complicated regular expression (for preg_replace)
Python Expression Evaluator Version 2
Echoing Titles
Perl server referencing
PHP - Find and Delete
PHP Regex
mod_rewrite code
HTML: Automatically add ALT tags
[JavaScript]: Checking for undefined values
My Favorite Portable Applications
Help with User Form Validation
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.