How to test user entered phone number start from 7

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    How to test user entered phone number start from 7

    I'm developing web using php and mysql. I want to validate phone number starting from 7 . How can i do that? Please help me.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Do you mean you want to make sure the phone number starts with 7?

    You could do this using ereg()

    Or more simply you could just do:
    [php]
    $lcPhoneNumber = $_POST['number'] ; // I'm assuming the number comes from a form

    $lcFirstChar = substr($lcPhone Number, 0, 1) ;

    if($lcFirstChar == '7')
    {
    // success
    }
    else
    {
    // fail
    }
    [/php]

    Any code you use should be wrapped in a bit more error checking to ensure there is a value set first of all but that's the basic idea.

    The advantage of ereg is that you can apply more advanced checks to the data supplied in one expression.

    Cheers
    nathj

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Exerpt from php.net
      Originally posted by php.net
      Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

      Comment

      • ghjk
        Contributor
        • Jan 2008
        • 250

        #4
        Thank you nathj. It works.

        Comment

        Working...