Results 1 to 4 of 4

Thread: regular expression problem

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default regular expression problem

    I have the following as a validation check on a search field,
    The person entries a name between 3 - 12 characters long and the function checks to see if it's only a string of letter and everything works fine.
    Now however I need to add in a single quote " ' " I've tried every combination I can think of and none of them work my best guess was [^a-zA-Z'] but that did not work and I've tried tons but get no where... help
    Peter

    original code:

    function is_alpha($someString) //function to check for letters
    {
    return (preg_match("/[^a-zA-Z]/", $someString) > 0) ? true : false;
    }

    $element = $_POST['name'];
    if (strlen($element) <3 or strlen($element) >12)// check letters entered is between 3-12
    {
    header("location:searchform.php?err=1"); // return error call
    }
    if(is_alpha($element)) //check that only letters entered
    {
    header("location:searchform.php?err=2"); // return error call
    }

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    This:

    Code:
    /[^a-zA-Z]/
    Matches any single character in the string that is not a-z or A-Z. And I see you are looking at it from the point a view that if there is a match, that means it's invalid. Now it might be as simple as escaping the ':

    Code:
    /[^a-zA-Z\']/
    But since the RE is already a string, a double escape may be required:

    Code:
    /[^a-zA-Z\\']/
    Have you tried those?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John

    Have tried both we no joy.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,730
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    I am terrible at functions, but try
    Code:
    /[^a-zA-Z\\\']/
    Last edited by james438; 12-06-2009 at 01:50 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

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