Forum Moderators: coopster

Message Too Old, No Replies

Help with validation function

         

FiRe

3:19 pm on Nov 11, 2008 (gmt 0)

10+ Year Member



I am trying to stop people from:

a) posting an email address anywhere in the message
b) posting a telephone number (between 11 and 13 consecutive digits) anywhere in the message

Here is the function I have that doesnt really work unless you put only an exact email or telephone number in. Can anyone make this better?

function contains_contact_details($msg) {
$out = false;
if (preg_match("/[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$/s", $msg)) {
$out = true;
}
else {
$msg = preg_replace("/([^0-9a-zA-Z])/", "", $msg);
if (preg_match('/^\d{11,13}$/', $msg)) $out = true;
}
return $out;
}

coopster

3:08 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A developer has already done some of the work for you in the PEAR RFC822 class:
Validate email ( format ) using preg_match() [webmasterworld.com]. Consecutive numbers should be fairly easy to pattern, variations on it should be considered as well such as parenthesis around the area code, dashes between, or perhaps periods between exchange and number, etc.