Check if a string only contains "A-Za-z0-9_" and "."

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stanleyhsieh
    New Member
    • Oct 2007
    • 4

    Check if a string only contains "A-Za-z0-9_" and "."

    Hi all!
    I'm new here!

    I was wondering how I can check if a string contains only "A-Za-z0-9_" and ".".
    I tried the following code

    [php]
    function check_validstri ng($check_valid string)
    {
    if(eregi('[A-Za-z0-9_.]',$check_valids tring))
    {
    return true;
    }
    else
    {
    return false;
    }
    [/php]

    The result is:

    When $check_validstr ing = 'aaa' it passed.
    When $check_validstr ing = '@@' it failed.
    But When $check_validstr ing = 'aaa@@' it passed.

    Any way to prevent this? Thanks!
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    have you looked at this?

    Comment

    • stanleyhsieh
      New Member
      • Oct 2007
      • 4

      #3
      Sure I did.
      I thought they are the same thing, other than the fact that eregi is not case-sensitive.

      Comment

      • MarkoKlacar
        Recognized Expert Contributor
        • Aug 2007
        • 296

        #4
        Hi,

        when I look at you code, it acts correctly.
        What I think you can do is add an extra if-condition explicitly looking for the characters that are not allowed.

        Let me know.

        Cheers

        Comment

        • stanleyhsieh
          New Member
          • Oct 2007
          • 4

          #5
          Thanks for your reply.
          I appriciated very very much.

          I would like to know, how can I use a if to prevent this from happening?
          Thanks!

          Comment

          • MarkoKlacar
            Recognized Expert Contributor
            • Aug 2007
            • 296

            #6
            Hi,

            well the if statement could look like this:

            [PHP]
            else if(eregi('[@]',$check_valids tring))
            return false;
            [/PHP]

            When you do:
            [PHP]
            if(eregi('A-Za-z0-9_.',$check_val idstring))
            [/PHP]

            The only thing you say is as long as there is an A or a etc the string is ok (it returns true).

            Now what you can do is look at this and read about how you can limit characters, you will find something useful trust me.

            Cheers

            Comment

            • stanleyhsieh
              New Member
              • Oct 2007
              • 4

              #7
              Thanks for you help!
              I finally got it working.

              Here's the code that should work.
              [PHP]
              function check_charators ($string)
              {
              if(ereg('^[A-Za-z0-9_][A-Za-z0-9_]*$', $string))
              {
              return true;
              }
              else
              {
              return false;
              }
              }
              [/PHP]

              I delete the "." ;)

              Comment

              Working...