Hi friends


I have a site made in PHP
Can someone please help with Email address validation?


Thanks

Dani AI

Generated

Several replies here try to solve validation with hand-rolled regex (see and ) or with client-side JS (see and ). Those approaches are useful for UX, but regexes are brittle (new TLDs, quoted local-parts, IDNs) and client-side checks must never be the only defense. Recommended workflow: do a simple, standards-aware server-side syntax check, optionally convert IDN domains to ASCII, optionally verify DNS (MX/A) for the domain, and finally confirm ownership by sending a one-time verification email.

A compact server-side pattern that follows this flow:

function email_is_valid($email) {
    $email = trim($email);

    // basic syntax (use PHP built-in)
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        return false;
    }

    // optional: convert internationalized domain names if available
    if (function_exists('idn_to_ascii')) {
        list($local, $domain) = explode('@', $email, 2);
        $ascii = idn_to_ascii($domain);
        if ($ascii === false) return false;
        $email = $local . '@' . $ascii;
    }

    // optional: check that the domain can receive mail
    $domain = substr(strrchr($email, "@"), 1);
    if (function_exists('checkdnsrr')) {
        if (!checkdnsrr($domain, 'MX') && !checkdnsrr($domain, 'A')) {
            return false;
        }
    } elseif (function_exists('dns_get_record')) {
        $mx = dns_get_record($domain, DNS_MX);
        if (empty($mx)) {
            $a = dns_get_record($domain, DNS_A + DNS_AAAA);
            if (empty($a)) return false;
        }
    }

    return true;
}

Notes and pitfalls: always trim input and do server-side checks; do not lowercase the local part (only the domain can be normalized); MX/A checks reduce typos but do not guarantee mailbox existence; SMTP verification is error-prone and can be blocked or misleading. The single reliable proof of control is a confirmation email with a time-limited token.

Recommended Answers

All 5 Replies

function validate_email($email)
{
    if (strlen($email) < 3) {
        return false;
    }
    if (!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',
        $email)) {
        return false;
    } else {
        return $email;
    }
}
function validate_email($email)
{
    if (strlen($email) < 3) {
        return false;
    }
    if (!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)) {
        return false;
    } else {
        return $email;
    }
}

This code is close, but does not allow addresses ending in .museum or .travel.

That's because the code limits the TLD to have between 2 and 4 characters. I would suggest relaxing the rules a little and not impose an upper limit (i.e. only a minimum of 2 characters). Thus line 6 ought to be:

if (!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,})$/',$email)) {

This code is close, but does not allow addresses ending in .museum or .travel.

That's because the code limits the TLD to have between 2 and 4 characters. I would suggest relaxing the rules a little and not impose an upper limit (i.e. only a minimum of 2 characters). Thus line 6 ought to be:

if (!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,})$/',$email)) {

Good point, forgot about those tlds

try below code

<script language="javascript">
function functionname()
{
 var emailFormat = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2})?$/i;
var email=document.formname.emailfieldname.value;
if(email=="")
	{
	alert("Please Enter The Email Id");
	document.formname.emailfieldname.focus();
	return false;
	}
if(email!= "")
		{	
		 	var eres=email.search(emailFormat);
			if(eres == -1)
			{
				alert("Please Enter Valid Email Id ");
				window.document.formname.emailfieldname.focus();
				return false;
			}
		} 
else
{
return true;
}
}

Hi friends


I have a site made in PHP
Can someone please help with Email address validation?


Thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">



var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
var returnval=emailfilter.test(e.value)
if (returnval==false){
alert("Please enter a valid email address.")
e.select()
}
return returnval
}

</script>


</head>

<body>
<form>
<input name="myemail" type="text" style="width: 270px"> <input type="submit" onClick="return checkmail(this.form.myemail)" value="Submit" />

</form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.