Results 1 to 12 of 12
  1. #1
    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

  2. #2
    Join Date
    Aug 2008
    Location
    Cyprus, EU
    Posts
    50
    PHP Code:
    if ([^0-9a-zA-Z.], $string)
    return 
    false

  3. #3
    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); 
    What we're saying here is if we match a character that is in the character set ([]) of not (^) a letter (a-z), a digit (\d) or a period (\.)--note that just a (.) in your regex will match on any character!--or white-space (\s) performing a case-insensitive search (i), then return the Boolean opposite of our match result, ie return false if the string contains an illegal character, otherwise true.

    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

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

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

  6. #6
    Join Date
    Sep 2007
    Posts
    815
    Quote Originally Posted by bigfan View Post
    PHP Code:
    $allowed_symbols = array('.');  // Add more as needed.
    if (!ctype_alnum(str_replace($allowed_symbols''$string)))
        return 
    false
    Doesn't this allow only period? how about entering alphanumeric? Curently I have this: #[^a-z0-9\s-]#i
    All things work together for the good of those who love God - Romans 8:28

  7. #7
    Join Date
    Nov 2005
    Posts
    123
    Quote Originally Posted by sharmaine1111 View Post
    Doesn't this allow only period?
    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.

  8. #8
    Join Date
    May 2009
    Posts
    766
    Quote Originally Posted by sharmaine1111 View Post
    what does \s mean? what does this mean: #[^a-z0-9\s-]#i such that: return !preg_match("#[^a-z0-9\s-]#i", $string) ?
    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

    If, like me, it makes more logical sense to you to have your validation function return true if the string is safe, and false if it contains bad characters, you will have to find the logical opposite of preg_match(), by preceding it with !

    PHP Code:
    if (isValid($testVal))
    {
      
    // value is safe
    }
    else
    {
     
    // handle error

    I'm interested in the efficiency difference between my solution and bigfan's...

  9. #9
    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
                    )

            )

    )

  10. #10
    Join Date
    Nov 2005
    Posts
    123
    Impressive. Almost .000178 seconds difference per function call.

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

  12. #12
    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 hood
    Ah, now I see. Thank you for helping me, for one, to understand you better.
    Last edited by bigfan; 09-27-2009 at 09:40 PM.

Similar Threads

  1. Input Text field present in SWF, but not source FLA
    By larwilliams in forum Web Design and Content
    Replies: 0
    Last Post: 03-25-2008, 09:28 PM
  2. Credit card type input field needed?
    By izzy10 in forum Ecommerce Hosting & Discussion
    Replies: 3
    Last Post: 06-26-2006, 10:13 PM
  3. c++ and printing symbols in input/output messages
    By Flumps in forum Programming Discussion
    Replies: 4
    Last Post: 12-10-2005, 12:22 PM
  4. input and output of textarea field
    By Lang14 in forum Programming Discussion
    Replies: 2
    Last Post: 05-24-2005, 12:00 PM
  5. Replies: 6
    Last Post: 08-23-2004, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •