Hi friends


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


Thanks

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.