Validating a Paragraphed String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basher
    New Member
    • May 2007
    • 1

    #1

    Validating a Paragraphed String

    Well I need a quick solution for checking if a string contains an email address or anything of the similar, example:

    I allow user to type in their personal profile in a textarea and name it "profile"

    so in php i will catch it in $profile but would want to check if there is an email value in it. If there is, an error will be trigered as this is not allowed.

    Please help.

    Thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, basher. Welcome to TSDN!

    You'll be wanting to use preg_match, and you'll probably like this article on searching for email addresses.

    Comment

    • Odisey
      New Member
      • May 2007
      • 14

      #3
      Originally posted by basher
      Well I need a quick solution for checking if a string contains an email address or anything of the similar, example:

      I allow user to type in their personal profile in a textarea and name it "profile"

      so in php i will catch it in $profile but would want to check if there is an email value in it. If there is, an error will be trigered as this is not allowed.

      Please help.

      Thanks
      Basher you want to use 'regular expressions'. Do a Google for recipe cut and paste scripts. Regular expressions in short are characters used - in this case - to constrain your data input to form. In other words, if a user does not input an email address as something@somep lace.ext they will get the }else{ error you code.

      Here is an example:

      I am using the eregi() function as !eregi and the eregi_replace. What this does is first determines if the input is a formated email address. If not, the error. If so, it recodifies it as a link. YOU can adjust it of course. Learn more about Regular Expressions!

      [CODE=php]

      //Check the mail address

      if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$',
      stripslashes(tr im($_POST['email'])))) {
      $problem = TRUE;
      $message .= '<p class="error">P lease enter a valid email address.</p>';

      } else {

      $email = eregi_replace ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', '<a href="mailto:\\ 0">Email</a>',
      stripslashes(tr im($_POST['email'])));

      }



      [/CODE]

      Marc
      Last edited by pbmods; May 20 '07, 04:18 PM. Reason: Changed code language. Thanks for using CODE tags!

      Comment

      Working...