How could I remove all the invalid email address from my mysql databse? ie ME.hotmails.com or thisguyyahoo.com or thisguy@yahoocom
| SitePoint Sponsor |




How could I remove all the invalid email address from my mysql databse? ie ME.hotmails.com or thisguyyahoo.com or thisguy@yahoocom





You'll want to select all applicable records, loop through them and check for validity. When you find an invalid one, store its ID. After you've gone through all of them, do a DELETE query on all the IDs you stored.
TuitionFree — a free library for the self-taught
Anode Says... — Blogging For Your Pleasure




Right. But how do I do that. What would be the php coding.
Also once that is done how can I stop people from entering invalid email addys? What snipet would I add to my existing php newsletter gathering form to make sure the email was a valid one?





You can do it 2 ways:
via JS:
Put the following in emailcheck.js
and for your form <form [other tags here] onSubmit="emailcheck.js"> , i tihnk.function emailCheck (emailStr)
{
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null)
return false
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null)
{
alert("The username doesn't seem to be valid.")
return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
{
for (var i=1;i<=4;i++)
{
if (IPArray[i]>255)
return false
}
return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null)
return false
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
domArr[domArr.length-1].length>4)
return false
if (len<2)
return false
return true;
}
Via php,
here's a quickie.
PHP Code:<?php
function validate($addy) {
if (preg_match("/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z0-9\-_]{2,6})$/i", $addy, $matches)) {
return 1;
} else {
return 0;
}
}
//script in action below
$addy = "sh@aaa.com";
if (validate($addy) == 0) { //function returns 0 if email addy is not valid
echo "Not valid";
} else {
echo "Valid";
}
?>




I want to use the PHP version, butI'm not sure how this is suppose to work. My form brings in the email addy as $email
Part of your code that confuses me is the part where you have an email address entered into the script 'sh@aaa.com'???





sh@aaa.com was just a dummy email address to check the function.
scrap this line : $addy = sh@aaa.com;
and replace $addy with $email
You can leave the function untouched.




That works awesome. Thank You.
Now how do I get rid of the ones I already have?
Bookmarks