!ereg not catching all cases

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BOMEz
    New Member
    • Dec 2007
    • 40

    !ereg not catching all cases

    I'm trying to validate some user entered text, and I'm using the code below:
    Code:
    $last_name = trim($_POST[‘last_name’]);
    if ( !ereg(“[A-Za-z’ -]{1,50}”,$last_name)
    {
    do stuff to require user to reenter last name;
    }
    Now if I enter the text :"John"..the text is accepted as it should be.
    Also if I just enter symbols such as or numbers...it gives the appropriate error message.

    The issue at hand is that if I were to enter something like "<john>" it also accepts the text. How can I fix this?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, BOMEz.

    You'll want to use boundary markers in your regular expression:
    [code=php]
    if ( !ereg("^[A-Za-z' -]{1,50}$",$last_ name)
    [/code]

    You should also consider using preg_match() instead of ereg(), as it is more efficient (http://php.net/preg_match, http://php.net/ereg).

    Comment

    • BOMEz
      New Member
      • Dec 2007
      • 40

      #3
      Thank you. I got it working

      I also looked into using preg_match instead.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime (:

        Comment

        Working...