Email Validation with REGEX in Javascript

Email Validation with REGEX in Javascript mytechlearns
JavaScriptLeave a Comment on Email Validation with REGEX in Javascript

Email Validation with REGEX in Javascript

Email Validation with REGEX in Javascript

Email validation is a very important part in a software. Most of the time user give a invalid email. We can check validity of email using a simple REGEX in javascript in font-end part. Let’s discuss about this with index.php page:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>JavaScript form validation - checking email</title>    
</head>
<body onload='document.form1.text1.focus()'>
	<div class="mail">
		<h2>Input an email and Submit</h2>
		<form name="form1" action="#"> 
			<input type='text' name='text1'/>
			<input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/>
		</form>
	</div>

	<script type="text/javascript">
		
		function ValidateEmail(inputText)
		{
			var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
			if(inputText.value.match(mailformat))
			{
				alert("You have entered a valid email address!");
				document.form1.text1.focus();
				return true;
			}
			else
			{
				alert("You have entered an invalid email address!");
				document.form1.text1.focus();
				return false;
			}
		}

	</script>

</body>
</html>

Here we use regular expression “/^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/” .

Happy Coding.

Source Code: Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top