-1

I have this php code

if(filter_var($_POST['username'], FILTER_SANITIZE_EMAIL) !== false && filter_var($_POST['username'], FILTER_VALIDATE_EMAIL) !== false) {
                echo 'it is mail';
            }else{
                echo 'is is phone';
            }

when I'm sending the post data like this

example@bla.com

it is writing it is mail. but when I'm sending the data like this

+995216846121

then it is writing also that it is mail

what is wrong? please help me.

I need to check if client add mail or phone number.

Marc B
  • 356,200
  • 43
  • 426
  • 500
shalvasoft
  • 89
  • 1
  • 9
  • filter_validate_email properly rejects the phone number here. try debugging your system: do a `var_dump(filter_var(...))` for BOTH of those options, and confirm that they're returning the proper true/false. – Marc B Apr 09 '15 at 15:09
  • now I try var_dump(filter_var($_POST['username'], FILTER_SANITIZE_EMAIL)); and in the both of them it returns string(here length) and the same string something is wrong can I convert this string in inteher like (int)$_POST['username'] and then check it ? or something like this? – shalvasoft Apr 09 '15 at 15:22
  • Are you sure that you have an error in this code. For me it's working fine – volkinc Apr 09 '15 at 15:59
  • yes I`m sure. it is not working. I give you my code how I`m checking. and it is not working. – shalvasoft Apr 09 '15 at 17:43
  • sanitize just cleans up and returns whatever's left of the string. validate returns a true/false... – Marc B Apr 09 '15 at 18:40

1 Answers1

0

I added one more check with is_numeric() and now everithing is good. thenk you All!!!

shalvasoft
  • 89
  • 1
  • 9
  • Another option would be regex. Then you can define own patterns for email and phone. – PKeidel Apr 10 '15 at 09:12
  • no regex not need. is_numeric class is doing what I needed. So if I send data e.g. +124654654 it will returns me true; – shalvasoft Apr 10 '15 at 09:24
  • I wrote `Another option`, not `the one and only` ;) If you can ensure that phone numers always have this format your solution is fine. But if someone would enter "+12 123123-34" is_numberic would return `false` because of the space and the "-". – PKeidel Apr 10 '15 at 09:27
  • So can you give me the regex code with removing minus,space and other characters what can user use in this case may be user add +(995) 5(58) 55-55-55 or I do not know. can you give me this regex? So I`ll be sure that user not will write anything more. – shalvasoft Apr 10 '15 at 09:31
  • I would just do an str_replace for anything <>[0-9], for the beginning. – PKeidel Apr 10 '15 at 09:37