craigtolputt Posted March 4, 2009 Share Posted March 4, 2009 Hi Guys, I needed to add some validation to a php script i am using some time ago and found the answer on here. the script i needed was to check the email field if the email address entered ended in .pnn.police.uk then it was ok tho send and if not then to return an error. here is the code i get //Custom function to check to see if email domain is equal to ".pnn.police.uk" function check_email($email) { return (substr(trim($email), -14) == '.pnn.police.uk'); } then if(check_email($email)) { //Start check email and then this at the end else { echo "&msgText=Please enter a valid police email address!"; //error message to display if $email doesnot contain ".pnn.police.uk" Now what i need to do is add another email extension to this aswell so would it be something like.. //Custom function to check to see if email domain is equal to ".pnn.police.uk" function check_email($email) { return (substr(trim($email), -14) == '.pnn.police.uk'); return (substr(trim($email), -18) == '.police.nsw.gov.au'); } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/ Share on other sites More sharing options...
WolfRage Posted March 4, 2009 Share Posted March 4, 2009 Is there a question here? Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776346 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 Yes... How can I add another email extension to this Code: [select]//Custom function to check to see if email domain is equal to ".pnn.police.uk" function check_email($email) { return (substr(trim($email), -14) == '.pnn.police.uk'); } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776348 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 Yes... How can I add another email extension to this //Custom function to check to see if email domain is equal to ".pnn.police.uk" function check_email($email) { return (substr(trim($email), -14) == '.pnn.police.uk'); } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776350 Share on other sites More sharing options...
phpdragon Posted March 4, 2009 Share Posted March 4, 2009 you could use in_array trim it first then use they in array function if (in_array($email, array('.pnn.police.uk', '.police.nsw.gov.au'))) { do this } else { do this } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776351 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 sorry im no good with PHP so would i still have function check_email($email) { return (substr(trim($email), -14) == '.pnn.police.uk'); } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776353 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 This is my script file, where would i add the array? [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776356 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 Out of curiosity... what's a sample email address that would pass this validation? Shouldn't they be ending with '@police.nsw.gov.au' rather than '.police.nsw.gov.au'? Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776365 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 yes Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776366 Share on other sites More sharing options...
phpdragon Posted March 4, 2009 Share Posted March 4, 2009 //Custom function to check to see if email domain is equal to ".pnn.police.uk" function check_email($email) { $emailcheck=substr(trim($email), -14); if (in_array($emailcheck, array('.pnn.police.uk', '.police.nsw.gov.au'))) { do this } else { do this } } Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776368 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 There's another way you could do it... one minute while I type Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776372 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 I'd do this: <?php function check_email($email) { $emailArray = explode("@", $email); if (($emailArray[1] == "pnn.police.uk")||($emailArray[1] == "police.nsw.gov.au")) { return (1); } } ?> So you can then throw something like this into your code: $email = "[email protected]"; if (check_email($email)) { echo "email is valid"; } There's probably a more elegant way to do the dual-condition if (I'm a newbie too) but exploding around the @ is a lot tidier than splitting based on string length, especially since it's going to vary depending on the extension. Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776374 Share on other sites More sharing options...
phpdragon Posted March 4, 2009 Share Posted March 4, 2009 the explode method would be a better option as your 2 email address extentions are different lengths are different lengths Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776382 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 thanks alot ill try this out now and let you guys know Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776383 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 This seemed to work fine but i still have an error which is with the pnn.police.uk email adresses they always have something after the @ so for example it could be [email protected] but the police.nsw.gov.au email addresses are always something like [email protected] so if i enter [email protected] or [email protected] it works fine but i need it slighty different Any Ideas?? Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776413 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 Try this out: // Build our function function check_email($email) { $emailArray = explode("@", $email); $ukArray = explode('.', $emailArray[1], 2); if (($emailArray[1] == "police.nsw.gov.au")||($ukArray[1] == "pnn.police.uk")) { return (1); } } // Let's test it out... $email = "[email protected]"; if (check_email($email)) { echo "email is valid"; } Not the most elegant solution ever Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776420 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 Ah super, that worked a treat. Thanks for all your help in fixing this. Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776426 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 Ah super, that worked a treat. Thanks for all your help in fixing this. Not a worry at all, glad to have helped. Shoot me an email or a PM if you're interested in knowing how any of it works. They're all really handy functions to know Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776430 Share on other sites More sharing options...
craigtolputt Posted March 4, 2009 Author Share Posted March 4, 2009 cheers, good to see help is here when im stuck. Im really a flash designer but ya know what its like when ya need to do something new. it all gets a bit, actually alot confusing. nice one Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776434 Share on other sites More sharing options...
47.46.45 Posted March 4, 2009 Share Posted March 4, 2009 Haha, I remember learning PHP and ActionScript at the same time... kept putting trace commands in PHP and echo commands in AS. Hurrr durrrr. Only switched to PHP when I started to move into really complicated stuff that AS was letting me down on. You should be fine with Flash for most tinkering hobby fun, and it's a nice easy language to get your head around the basics of programming. Enjoy Link to comment https://forums.phpfreaks.com/topic/147923-solved-adding-an-if-else-or-something/#findComment-776440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.