Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Regular Expression Question

    I am having a problem with validation for a form which uses regular expressions for validation purposes. I would like to allow line breaks in a text area field, but haven't had any luck so far. Any advice is appreciated. Thanks.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by zach4618 View Post
    I am having a problem with validation for a form which uses regular expressions for validation purposes. I would like to allow line breaks in a text area field, but haven't had any luck so far. Any advice is appreciated. Thanks.
    I guess you can post the relevant part of the code.

    regards

  • #3
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So here is the regular expression:
    PHP Code:
    if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$"$message)) {$messageError=1$error=1;} 
    What I would like to do is add line breaks to the allowed characters. IE:

    "Hello,
    Here is my message"

    This would currently return an error in the form. I would like to add to the regular expression to address that.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by zach4618 View Post
    So here is the regular expression:
    PHP Code:
    if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$"$message)) {$messageError=1$error=1;} 
    What I would like to do is add line breaks to the allowed characters. IE:

    "Hello,
    Here is my message"

    This would currently return an error in the form. I would like to add to the regular expression to address that.
    in php manual is a warning regarding ereg....:
    These regular expression functions are not binary-safe. The PCRE functions are.
    try this:
    PHP Code:
    if(!preg_match("/^([\w\s.,?()-]+)$/m",$message)){
      print 
    "your error message or code here";

    regards
    Last edited by oesxyl; 08-02-2008 at 04:37 PM.

  • Users who have thanked oesxyl for this post:

    zach4618 (08-03-2008)

  • #5
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the post - that solved my initial problem. However a new one was created. Later in the form I use a header function to redirect to a new page:

    PHP Code:
    header ('Location: success.php?message=' $message ''); 
    If I replace the eregi function like you suggested, I get the following error message when I try and submit the form:

    Warning: Header may not contain more than a single header, new line detected. in /home/.usual/artifexproductions/artifexproductions/contact.php on line 51

    If I revert to the eregi function this error goes away.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by zach4618 View Post
    Thanks for the post - that solved my initial problem. However a new one was created. Later in the form I use a header function to redirect to a new page:

    PHP Code:
    header ('Location: success.php?message=' $message ''); 
    If I replace the eregi function like you suggested, I get the following error message when I try and submit the form:

    Warning: Header may not contain more than a single header, new line detected. in /home/.usual/artifexproductions/artifexproductions/contact.php on line 51

    If I revert to the eregi function this error goes away.
    wrong conclusion, wrong solution!
    replacing something that don't work with something that work has changed the logic. You just discover that you have more then one problem in your script.

    regards

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    $message from a preg_match or preg_match_all will be an array not a string. You will likely want to use a $message[0] for you're result, but its hard to say for certain. Dump out the contents of $message and use it to determine what you need to display.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    $message from a preg_match or preg_match_all will be an array not a string. You will likely want to use a $message[0] for you're result, but its hard to say for certain. Dump out the contents of $message and use it to determine what you need to display.
    I miss something?
    preg_match here have only two arguments, that means first is the pattern, second the subject and it return false or true if it match or not. Just test a condition,
    I have no idea how preg_match_all can be used here, is a validation only.
    second problem is because the logic was changed.

    PS: I expect from OP to post the code if need help.

    regards
    Last edited by oesxyl; 08-02-2008 at 08:35 PM.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by oesxyl View Post
    I miss something?
    preg_match here have only two arguments, that means first is the pattern, second the subject and it return false or true if it match or not. Just test a condition,
    I have no idea how preg_match_all can be used here, is a validation only.
    second problem is because the logic was changed.

    PS: I expect from OP to post the code if need help.

    regards
    Nope, my bad, I was thinking on matches which doesn't exist here (only two args). Thats what I get for trying to help when I first wake up
    In that case, try urlencoding and trimming you're message before putting it in the header.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    Nope, my bad, I was thinking on matches which doesn't exist here (only two args). Thats what I get for trying to help when I first wake up
    anybody have right to be wrong, including as,

    Quote Originally Posted by zach4618 View Post
    So here is the regular expression:
    PHP Code:
    if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$"$message)) {$messageError=1$error=1;} 
    What I would like to do is add line breaks to the allowed characters. IE:

    "Hello,
    Here is my message"

    This would currently return an error in the form. I would like to add to the regular expression to address that.
    In that case, try urlencoding and trimming you're message before putting it in the header.
    I don't make any connection untill you said that.
    I guess this is the solution.

    regards

  • #11
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    wrong conclusion, wrong solution!
    replacing something that don't work with something that work has changed the logic. You just discover that you have more then one problem in your script.

    regards
    Actually I am replacing something that works with something that doesn't. The form works fine until I add the preg_match function.

    Quote Originally Posted by Fou-Lu View Post
    In that case, try urlencoding and trimming you're message before putting it in the header.
    What function should I use to accomplish this?

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by zach4618 View Post
    Actually I am replacing something that works with something that doesn't. The form works fine until I add the preg_match function.
    is my fault. I will avoid to do "something that doesn't" work for you.

    What function should I use to accomplish this?

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The function trim() to remove any trailing or starting whitespace (including \r\n\c characters), and urlencode() / urldecode() for sending and retrieving the message from the $_GET.
    PHP Code:
     header ('Location: success.php?message=' urlencode(trim($message)) . ''); 
    When displaying the message from the sucess.php page, use urldecode($message) to reformat the string. The problem is that url doesn't like having spaces and a few other characters in it. This allows you to treat it as a value the GET can understand and keep together correctly for later extraction.
    This is why I like post. Too bad its really tricky to send it as a header >.<

    This of course will only work if the preg_matching is correct.

    Edit:
    I'm looking at this pattern as well. A developer who has more experience with patterns may need to confirm, but I don't believe that [:alnum:] is available for use in preg. Instead, using a range: [a-z0-9] with an insensitive flag would provide the same usage:
    PHP Code:
    $pattern "/^([a-z0-9]|-|\.|,|\(|\)|\?|')+$/mi"
    This sould say any character that is {a-zA-Z0-9}-.,()? or ' at least once.
    Like I said, I don't know for certain if alnum is unavailable. I'm guessing its a part of POSIX, not PCRE, but I could be wrong.
    Last edited by Fou-Lu; 08-02-2008 at 11:04 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    zach4618 (08-03-2008)

  • #14
    New Coder
    Join Date
    Feb 2007
    Location
    Albany, Oregon
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you both for your helpful responses.

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    NP.
    I'm guessing you've got it figured out
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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