validate email in PHP

Here’s a simple script to validate an email address. This only checks for structure, and doesn’t confirm the validity of the address (ie does it actually exist).

<?php
function validate($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
?>

[Edit – 24.07.09: I’ve updated this script as I found some more detail of email address definitions, ie RFC 2822]

I’m working on a “confirmation” script which will ping or otherwise check the submitted email address for validity as an actual existing address.

This post here also has an even better email validator script (Listing 9) which also tests the email domain against a DNS.

generate random password in PHP

Just thought I’d share this little script for generating a random password with PHP. You can customise the length and strength of the password when calling the method.

<?php
function generatePassword($length, $strength) {
$vowels = 'aeiouy';
$consonants = 'bcdfghjklmnpqrstvwxz';
if ($strength == 1) {
$consonants .= 'BCDFGHJKLMNPQRSTVWXZ';
}
if ($strength == 2) {
$vowels .= "AEIOUY";
}
if ($strength == 4) {
$consonants .= '123456789';
}
if ($strength == 8 ) {
$consonants .= '@#$%';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
}
else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
?>

you then simply call the function from within your script as:

<?php
$newPass = generatePassword(length, strength);
?>

And there you have it, a randomly generated password. You can play with the strength factros and variables if you wish; entirely up to you.

trimming white space in JS easily

I’m still crook, as my Twitter tweets will have let you know, but I thought I’d jump online and do a quick post. I got up this morning feeling worse than ever, but ecided to give lectures a go. Got showered dressed and out the door, before the migraine and sinus went “MEH!!!”; so I got some Night-and-Day and the pharmacy and went home to bed….

Anyways, here’s a quick script to deal with white-space in JavaScript strings, and remove both leading and trailing white-space with one easy function:

<script language="javascript">
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
</script>

Nothing flash or earth-shattering here, just thought it might be useful.

I’ll try to be coherent later, and post some more tuts. I won’t have much to say myself, as talking about sleeping and being in bed is boring for everyone, including the writer (me).