Validating Email Addresses with Regular Expressions in PHP

Validating Email Addresses with Regular Expressions in PHP

Most Web forms designed in PHP are made for user interaction. This generally means information exchange, and the most common way this is done is via email.

Because of this, validating email addresses for misprints or false entries is crucial for succesful interactions.

The following class is small and very easy to use. It uses regular expressions for validating email adresses.

-------Class------------------------------------------------class email{function valid($str){	if (ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)+$", $str))	{		return 1;	}	else       	{		return 0; 	}}}---------Example usage---------------------------------------$check_valid = new email;$mail="[email protected]";if($check_valid->valid($mail)){  echo "
Email is valid";}else { echo "
Email is not valid";}
See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist