Finding @ in a string, and other items

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

    Finding @ in a string, and other items

    Ok - I want to check a string for @ in it (Im checking to see if a string
    that should contain an email holds a @ )

    How would I go about this? preg_match? ereg? strstr?Examples ?

    Also - how would I find any non alpha numeric characters in a string so I
    can give the user an error message. I dont want any characters besides
    alpha-numeric in the strings.


  • Chung Leong

    #2
    Re: Finding @ in a string, and other items

    It's easiest to use regexp to deal with this sort of thing.
    preg_match('/^[\w\.]+@\w+\.\w+/', $address) should do the trick.

    Uzytkownik "Picture Dots" <webmaster@brim stonedragons.co m> napisal w
    wiadomosci news:HQXVb.9637 $se3.4553@fe1.c olumbus.rr.com. ..[color=blue]
    > Ok - I want to check a string for @ in it (Im checking to see if a string
    > that should contain an email holds a @ )
    >
    > How would I go about this? preg_match? ereg? strstr?Examples ?
    >
    > Also - how would I find any non alpha numeric characters in a string so I
    > can give the user an error message. I dont want any characters besides
    > alpha-numeric in the strings.
    >
    >[/color]


    Comment

    • Geoff Berrow

      #3
      Re: Finding @ in a string, and other items

      I noticed that Message-ID: <woGdnXBYuMT89r XdRVn-ug@comcast.com> from
      Chung Leong contained the following:
      [color=blue]
      >It's easiest to use regexp to deal with this sort of thing.
      >preg_match('/^[\w\.]+@\w+\.\w+/', $address) should do the trick.[/color]

      Reg exp and easy in the same sentence?

      if (!strpos($email ,"@"))
      {
      print "try again, email addresses must contain \"@\"";
      }
      else{
      print "That's a fine email address!";
      }

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Michel

        #4
        Re: Finding @ in a string, and other items

        Your solution does not provide any check other than checking for existance
        of @
        The regexp does :)

        Mich

        "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
        news:gi7h20tg2l k9v4g433f1tgc4q mnm9d45cf@4ax.c om...[color=blue]
        > I noticed that Message-ID: <woGdnXBYuMT89r XdRVn-ug@comcast.com> from
        > Chung Leong contained the following:
        >[color=green]
        > >It's easiest to use regexp to deal with this sort of thing.
        > >preg_match('/^[\w\.]+@\w+\.\w+/', $address) should do the trick.[/color]
        >
        > Reg exp and easy in the same sentence?
        >
        > if (!strpos($email ,"@"))
        > {
        > print "try again, email addresses must contain \"@\"";
        > }
        > else{
        > print "That's a fine email address!";
        > }
        >
        > --
        > Geoff Berrow (put thecat out to email)
        > It's only Usenet, no one dies.
        > My opinions, not the committee's, mine.
        > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


        Comment

        • John Dunlop

          #5
          Re: Finding @ in a string, and other items

          Michel wrote upsidedown:
          [color=blue]
          > "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
          > news:gi7h20tg2l k9v4g433f1tgc4q mnm9d45cf@4ax.c om...
          >[color=green]
          > > if (!strpos($email ,"@")) [...][/color]
          >
          > Your solution does not provide any check other than checking for existance
          > of @[/color]

          One of the queries was how to go about checking a string contained
          "@". Using regular expression functions for that alone would be
          inefficient.
          [color=blue]
          > The regexp does :)[/color]

          Indeed. But it doesn't answer the other query (how to detect non-
          alphanumeric characters in a string). So why use it?

          I suspect Picture Dots is trying to confirm the validity of email
          addresses, but neither the Subject line nor the body of the original
          article says so. The pattern in question is insufficient for
          checking email address syntax.

          --
          Jock

          Comment

          • Geoff Berrow

            #6
            Re: Finding @ in a string, and other items

            I noticed that Message-ID: <c0ajk1$ac$1@ne ws.cistron.nl> from Michel
            contained the following:
            [color=blue]
            >Your solution does not provide any check other than checking for existance
            >of @
            >The regexp does :)[/color]

            I know. But I think such checks are pointless anyway.
            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Chung Leong

              #7
              Re: Finding @ in a string, and other items

              More or less. Mistyping a email address that's synatically correct is far
              more likely than enter a invalid character. Doing a MX lookup might be more
              worthwhile than trying to write a regexp that validates RFC822 conformance.

              Uzytkownik "Geoff Berrow" <blthecat@ckdog .co.uk> napisal w wiadomosci
              news:bkmh20pd2c mdlra2hsdaglate uiaajdfpg@4ax.c om...[color=blue]
              > I noticed that Message-ID: <c0ajk1$ac$1@ne ws.cistron.nl> from Michel
              > contained the following:
              >[color=green]
              > >Your solution does not provide any check other than checking for[/color][/color]
              existance[color=blue][color=green]
              > >of @
              > >The regexp does :)[/color]
              >
              > I know. But I think such checks are pointless anyway.
              > --
              > Geoff Berrow (put thecat out to email)
              > It's only Usenet, no one dies.
              > My opinions, not the committee's, mine.
              > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


              Comment

              • Rahul Anand

                #8
                Re: Finding @ in a string, and other items

                "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<hsKdndUVw 6CMX7fdRVn-jQ@comcast.com> ...[color=blue]
                > More or less. Mistyping a email address that's synatically correct is far
                > more likely than enter a invalid character. Doing a MX lookup might be more
                > worthwhile than trying to write a regexp that validates RFC822 conformance.
                >
                > Uzytkownik "Geoff Berrow" <blthecat@ckdog .co.uk> napisal w wiadomosci
                > news:bkmh20pd2c mdlra2hsdaglate uiaajdfpg@4ax.c om...[color=green]
                > > I noticed that Message-ID: <c0ajk1$ac$1@ne ws.cistron.nl> from Michel
                > > contained the following:
                > >[color=darkred]
                > > >Your solution does not provide any check other than checking for[/color][/color]
                > existance[color=green][color=darkred]
                > > >of @
                > > >The regexp does :)[/color]
                > >
                > > I know. But I think such checks are pointless anyway.
                > > --
                > > Geoff Berrow (put thecat out to email)
                > > It's only Usenet, no one dies.
                > > My opinions, not the committee's, mine.
                > > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color][/color]

                You can find a regular expression to validate email-address here:



                Try using this if it worth using for your email-validation :)

                --
                Thanks,
                Rahul Anand

                Comment

                • John Dunlop

                  #9
                  Re: Finding @ in a string, and other items

                  Rahul Anand wrote:
                  [color=blue]
                  > You can find a regular expression to validate email-address here:
                  >
                  > http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html[/color]

                  This is a Perl module which validates email addresses against the
                  syntax of RFC822 (obsoleted by RFC2822); the pattern itself will not
                  match all valid email addresses, despite its complexity.
                  [color=blue]
                  > Try using this if it worth using for your email-validation :)[/color]

                  Validating the syntax of an email address and finding it exists is
                  not proof of it being in use. If you'd like to know that what a user
                  entered is really her email address, then a confirmation mail is the
                  best way, as both the CGI FAQ and the Mail::RFC822::A ddress
                  documentation recommend.

                  --
                  Jock

                  Comment

                  • Rahul Anand

                    #10
                    Re: Finding @ in a string, and other items

                    John Dunlop <john+usenet@jo hndunlop.info> wrote in message news:<MPG.1a95c 2465fa6d64a9896 8d@News.Individ ual.NET>...[color=blue]
                    > Validating the syntax of an email address and finding it exists is
                    > not proof of it being in use. If you'd like to know that what a user
                    > entered is really her email address, then a confirmation mail is the
                    > best way, as both the CGI FAQ and the Mail::RFC822::A ddress
                    > documentation recommend.[/color]


                    Yes, I agree. Best way is to send a mail with confirmation URL or some
                    confirmation code. Only after actual validation you can be sure that
                    the request is authentic.

                    --
                    Cheers,
                    Rahul Anand

                    Comment

                    • Chung Leong

                      #11
                      Re: Finding @ in a string, and other items

                      I just thought of a reason to validate the syntax of a email address. If the
                      address is going to be placed in a hyperlink that is displayed publically,
                      with validation an attacker can inject Javascript into your page:

                      <a href="mailto: $address">

                      if $address is "><script src="123.45.2.1/a.js">

                      then the output becomes

                      <a href="mailto: "><script src="123.45.2.1/a.js">">

                      Uzytkownik "John Dunlop" <john+usenet@jo hndunlop.info> napisal w wiadomosci
                      news:MPG.1a95c2 465fa6d64a98968 d@News.Individu al.NET...[color=blue]
                      > Rahul Anand wrote:
                      >[color=green]
                      > > You can find a regular expression to validate email-address here:
                      > >
                      > > http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html[/color]
                      >
                      > This is a Perl module which validates email addresses against the
                      > syntax of RFC822 (obsoleted by RFC2822); the pattern itself will not
                      > match all valid email addresses, despite its complexity.
                      >[color=green]
                      > > Try using this if it worth using for your email-validation :)[/color]
                      >
                      > Validating the syntax of an email address and finding it exists is
                      > not proof of it being in use. If you'd like to know that what a user
                      > entered is really her email address, then a confirmation mail is the
                      > best way, as both the CGI FAQ and the Mail::RFC822::A ddress
                      > documentation recommend.
                      >
                      > --
                      > Jock[/color]


                      Comment

                      • John Dunlop

                        #12
                        Re: Finding @ in a string, and other items

                        Chung Leong wrote upsidedown:
                        [color=blue]
                        > I just thought of a reason to validate the syntax of a email address. If the
                        > address is going to be placed in a hyperlink that is displayed publically,
                        > with validation an attacker can inject Javascript into your page:
                        >
                        > <a href="mailto: $address">
                        >
                        > if $address is "><script src="123.45.2.1/a.js">
                        >
                        > then the output becomes
                        >
                        > <a href="mailto: "><script src="123.45.2.1/a.js">">[/color]

                        If you're inserting user-supplied data into your document, and only
                        checking said data conforms to RFC(2)822 address syntax, you've still
                        got problems. Imagine this syntactically valid address in the
                        situation above:

                        "><script src='scheme:123 .45.2.1/a.js'>"@domain. example

                        (It's essential to use an absolute URI, otherwise the src value will
                        be interpreted as a relative URI. Adding the required type
                        attribute, obligatory SCRIPT end-tag, and the A element's end-tag,
                        could conceivably result in a valid document!)

                        Presumably, however, if the address is to be used as part of an URL,
                        you'd URL encode it, after separating it into its component parts.
                        This is a hypothetical discussion. :-)

                        --
                        Jock

                        Comment

                        Working...