How to check if a character appears in a form variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • monomaniac21

    How to check if a character appears in a form variable?

    Hi guys!

    Can anyone tell me how you can perform validation on a string variable
    in PHP so that you can check if a specific character is contained
    within the string. The reason for this is i am trying to create a form
    for a site i am making and i want to validate the user's input for the
    email address field, by ensuring it contains the '@' character.

    Cheers.

  • Malcolm Dew-Jones

    #2
    Re: How to check if a character appears in a form variable?

    monomaniac21 (marcrice20@msn .com) wrote:
    : Hi guys!

    : Can anyone tell me how you can perform validation on a string variable
    : in PHP so that you can check if a specific character is contained
    : within the string. The reason for this is i am trying to create a form
    : for a site i am making and i want to validate the user's input for the
    : email address field, by ensuring it contains the '@' character.

    That is hardly a check to validate an email address!

    However, one function that would help is strstr();

    $at = strstr($claimed _email_address, "@");
    if ($at == "")
    {
    echo "no @ found";
    die ;
    }

    But there are dozens of other functions that would also help. The php
    manual is available on line (google for it) look for string functions.


    --

    This programmer available for rent.

    Comment

    • Mazin07

      #3
      Re: How to check if a character appears in a form variable?

      Instead, use preg_match to use Perl-style regular expressions.
      It can check to see if it found a pattern.
      For emails, use something like:

      if( preg_match("/\w+@\w+\.\w+",$ variable) )
      {
      do_whatever...;
      }

      which will check for the format abc@abc.abc

      Comment

      • John Dunlop

        #4
        E-mail address syntax validation with regular expressions

        Mazin07 wrote:
        [color=blue]
        > /\w+@\w+\.\w+[/color]

        Ad hoc regular expressions to validate the syntax of e-mail addresses
        are invariably both too loose and too strict. Too loose in that they
        pass forms not allowed by RFC2822, too strict in that they reject
        forms allowed by RFC2822. Even the Mail::RFC822::A ddress pattern
        doesn't encompass the gamut of forms, but then it doesn't pretend to.

        --
        Jock

        Comment

        • Tim Roberts

          #5
          Re: E-mail address syntax validation with regular expressions

          John Dunlop <usenet+2004@jo hn.dunlop.name> wrote:[color=blue]
          >
          >Mazin07 wrote:
          >[color=green]
          >> /\w+@\w+\.\w+[/color]
          >
          >Ad hoc regular expressions to validate the syntax of e-mail addresses
          >are invariably both too loose and too strict. Too loose in that they
          >pass forms not allowed by RFC2822, too strict in that they reject
          >forms allowed by RFC2822. Even the Mail::RFC822::A ddress pattern
          >doesn't encompass the gamut of forms, but then it doesn't pretend to.[/color]

          Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
          regular expression which matches all valid RFC 822 e-mail addresses. It is
          more than 6,500 characters long.
          --
          - Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          • John Dunlop

            #6
            Re: E-mail address syntax validation with regular expressions

            Tim Roberts wrote:
            [color=blue]
            > Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
            > regular expression which matches all valid RFC 822 e-mail addresses. It is
            > more than 6,500 characters long.[/color]

            Interesting. I don't have his book, but is that the pattern
            Email::Valid uses?



            --
            Jock

            Comment

            • Tim Roberts

              #7
              Re: E-mail address syntax validation with regular expressions

              John Dunlop <usenet+2004@jo hn.dunlop.name> wrote:
              [color=blue]
              >Tim Roberts wrote:
              >[color=green]
              >> Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
              >> regular expression which matches all valid RFC 822 e-mail addresses. It is
              >> more than 6,500 characters long.[/color]
              >
              >Interesting. I don't have his book, but is that the pattern
              >Email::Valid uses?
              >
              >http://search.cpan.org/src/MAURICE/E...-0.15/Valid.pm[/color]

              That's the one. He credits Friedl in the code.

              The Friedl book is actually quite good. Some people think the subject
              could be adequately covered in a 3-page man page, but Friedl covers the
              tricks, ins, and outs of many of the popular regular expression engines. I
              recommend it.
              --
              - Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • John Dunlop

                #8
                Re: E-mail address syntax validation with regular expressions

                Tim Roberts wrote:
                [color=blue]
                > The Friedl book is actually quite good.[/color]

                [...]
                [color=blue]
                > I recommend it.[/color]

                It's on my wish list, Tim, right below 25-hour days.

                --
                Jock

                Comment

                Working...