Results 1 to 12 of 12
-
09-24-2009, 11:25 AM #1Web Hosting Master
- Join Date
- Sep 2007
- Posts
- 815
php help on checking the value of the input field without symbols but period allowed?
I currently have a checking in my form input field that it should contain alphanumeric characters only, so not a single symbol is allowed However I want to revise it and disallow all symbols except period (.) how do i do that?
All things work together for the good of those who love God - Romans 8:28
-
09-24-2009, 01:05 PM #2Junior Guru Wannabe
- Join Date
- Aug 2008
- Location
- Cyprus, EU
- Posts
- 50
PHP Code:if ([^0-9a-zA-Z.], $string)
return false;
-
09-24-2009, 03:50 PM #3Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
or if you're doing this server-side (which you should be), here's the PHP equivalent:
PHP Code:return !preg_match("/[^a-z\d\.\s]/i", $string);
You should also note that \w is a short hand for matching alpha-numeric characters, but it allows the underscore character in that set. If that's okay, you could get away with /[^\w\.\s]/.Last edited by mattle; 09-24-2009 at 04:00 PM. Reason: added explanation of regex
-
09-25-2009, 10:51 AM #4Web Hosting Master
- Join Date
- Sep 2007
- Posts
- 815
what does \s mean? what does this mean: #[^a-z0-9\s-]#i such that: return !preg_match("#[^a-z0-9\s-]#i", $string) ?
All things work together for the good of those who love God - Romans 8:28
-
09-25-2009, 04:38 PM #5WHT Addict
- Join Date
- Nov 2005
- Posts
- 123
PHP Code:$allowed_symbols = array('.'); // Add more as needed.
if (!ctype_alnum(str_replace($allowed_symbols, '', $string)))
return false;
Last edited by bigfan; 09-25-2009 at 04:46 PM.
-
09-25-2009, 09:02 PM #6Web Hosting Master
- Join Date
- Sep 2007
- Posts
- 815
-
09-26-2009, 04:20 AM #7WHT Addict
- Join Date
- Nov 2005
- Posts
- 123
It will allow both alphanumeric characters and periods, which is my understanding of what you want to do. If you try it you'll see.
PHP Code:function is_valid_string($string)
{
$allowed_symbols = array('.'); // Add more as needed.
return ctype_alnum(str_replace($allowed_symbols, '', $string));
}
// Test code:
foreach (array('good.string', 'bad*string') as $str) {
if (is_valid_string($str)) {
echo $str . ' -- valid<br />';
} else {
echo $str . ' -- not valid<br />';
}
}
// Result:
// good.string -- valid
// bad*string -- not valid
Last edited by bigfan; 09-26-2009 at 04:33 AM.
-
09-26-2009, 12:26 PM #8Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
Sorry, I thought I explained that. The \s means that you are allowing white-space (spaces, tabs, etc.) There are basic components to this regex that I think need explaining...
[...] -- character set, regex will match if any character between the brackets matches
[^...] -- negated character set, regex will match if there is a character that is NOT within the braces, not to be confused with...
^[...] -- regex will match a string that BEGINS with that character set.
Now, within the character set, we have some ranges:
a-z -- match any lowercase* alphanumeric character
0-9 -- match any digit
And some specific allowable characters:
\s -- match any white-space
- -- match a hyphen (from your expression)
\. -- match a period (from the OP requirements)
Finally, we give a parameter to the expression (outside of the delimiters):
i -- perform a case-insensitive search. * This is why it's okay to match on just lowercase characters above.
A character set will match on any character in the brackets, therefore the expression
[a-z0-9\s\.-] will match any letter, number, space, hyphen or period in the expression.
Likewise, [^a-z0-9\s\.-] will match any character that is NOT a letter, number, space, hyphen or period.
That means that preg_match() on the expression above will return TRUE if there is a character that is not allowed. That alone is fine. If you functionalize the procedure to check for validity, you might have something like this:
PHP Code:if (hasIllegalChars($testVal))
{
//handle error
}
else
{
// value is safe
}
PHP Code:if (isValid($testVal))
{
// value is safe
}
else
{
// handle error
}
-
09-26-2009, 12:55 PM #9Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
For those who are interested...
PHP Code:# cat timing.php
<?
$testStrings = array("A 28 character valid string.", "shrt_inv", "_inv at beginning",
"inv at end{", "a fairly long, and nonetheless, invalid string expression",
"a fairly long. and nonetheless. ..valid string expression");
$functions = array("regex", "ctype");
$results = array();
$executionCount = 10000;
foreach ($functions as $fn)
{
$results[$fn]['total'] = 0;
foreach ($testStrings as $str)
{
$start = microtime(true);
$result = NULL;
for ($i = 0; $i < $executionCount; ++$i)
{
$result = $fn($str);
}
$results[$fn][$str]['result'] = $result;
$results[$fn][$str]['ms'] = (microtime(true) - $start) * 1000;
$results[$fn]['total'] += $results[$fn][$str]['ms'];
}
}
print_r($results);
function regex($string)
{
return !preg_match("/[^a-z0-9\s\.-]/i", $string);
}
function ctype($string)
{
$allowed_symbols = array('.', '-', ' ');
return ctype_alnum(str_replace($allowed_symbols, '', $string));
}
?>
# php -f timing.php
Array
(
[regex] => Array
(
[total] => 398.58436584473
[A 28 character valid string.] => Array
(
[result] => 1
[ms] => 71.012020111084
)
[shrt_inv] => Array
(
[result] =>
[ms] => 59.103012084961
)
[_inv at beginning] => Array
(
[result] =>
[ms] => 56.730031967163
)
[inv at end{] => Array
(
[result] =>
[ms] => 62.520027160645
)
[a fairly long, and nonetheless, invalid string expression] => Array
(
[result] =>
[ms] => 64.160108566284
)
[a fairly long. and nonetheless. ..valid string expression] => Array
(
[result] => 1
[ms] => 85.05916595459
)
)
[ctype] => Array
(
[total] => 577.25596427917
[A 28 character valid string.] => Array
(
[result] => 1
[ms] => 96.320867538452
)
[shrt_inv] => Array
(
[result] =>
[ms] => 91.674089431763
)
[_inv at beginning] => Array
(
[result] =>
[ms] => 93.331098556519
)
[inv at end{] => Array
(
[result] =>
[ms] => 93.492984771729
)
[a fairly long, and nonetheless, invalid string expression] => Array
(
[result] =>
[ms] => 98.383903503418
)
[a fairly long. and nonetheless. ..valid string expression] => Array
(
[result] => 1
[ms] => 104.05302047729
)
)
)
-
09-26-2009, 02:21 PM #10WHT Addict
- Join Date
- Nov 2005
- Posts
- 123
Impressive. Almost .000178 seconds difference per function call.
-
09-27-2009, 10:30 AM #11Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
I think you got killed by the call to str_replace() I really wasn't trying to prove the validity of one solution over the other, I've just always been impressed by the efficiency of regular expressions. Back in my Perl days, we used to always time functions. Just curious how PHP regex's stacked up against the ctime functions.
I think there's a lot more information to be gleaned from this..for example, the ctime expression executed consistently on similar-length strings, regardless of where the offending character was, whereas the regular expression clearly operates faster if the illegal character is near the front.
I know a lot of people choose PHP because there's a lot of magic that happens without the programmer needing to do a lot of work--or understand what's going on internally. I, for one, am still interested in what's going on under the hood
-
09-27-2009, 09:37 PM #12WHT Addict
- Join Date
- Nov 2005
- Posts
- 123
I know a lot of people choose PHP because there's a lot of magic that happens without the programmer needing to do a lot of work--or understand what's going on internally. I, for one, am still interested in what's going on under the hoodLast edited by bigfan; 09-27-2009 at 09:40 PM.
Similar Threads
-
Input Text field present in SWF, but not source FLA
By larwilliams in forum Web Design and ContentReplies: 0Last Post: 03-25-2008, 09:28 PM -
Credit card type input field needed?
By izzy10 in forum Ecommerce Hosting & DiscussionReplies: 3Last Post: 06-26-2006, 10:13 PM -
c++ and printing symbols in input/output messages
By Flumps in forum Programming DiscussionReplies: 4Last Post: 12-10-2005, 12:22 PM -
input and output of textarea field
By Lang14 in forum Programming DiscussionReplies: 2Last Post: 05-24-2005, 12:00 PM -
Checking for the value of an Image Type form field in php when submited?
By Barti1987 in forum Programming DiscussionReplies: 6Last Post: 08-23-2004, 04:33 PM