Jump to content

[SOLVED] Adding an If Else or something???


craigtolputt

Recommended Posts

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

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');

}

//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 }

}

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.

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??

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 ;)

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 :)

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 ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.