Email ID validation in JavaScript can be achieved using regular expressions(regex) to check if the input follows the standard format of an email address. It is a common task in web development to ensure that users provide a valid email address before submitting a form.
How to verify Email ID validation using JavaScript
Here are the steps:
- Create a regular expression that matches the format of an email address. The regular expression should cover basic requirements such as the presence of “@” symbol and a domain with at least one dot.
- Implement a JavaScript function that takes an an email address as input and uses the regular expression to test its validity.
- Test the validation function with various email addresses to ensure it correctly identifies valid and invalid email formats.
function validateEmail(email) { const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return regex.test (email); } const email1 = "[email protected]"; const email2 = "apple-email"; console.log('Email: ${email1} is ${validateEmail(email1) ? 'Valid' : 'Invalid'}); console.log('Email: ${email2} is ${validateEmail(email2) ? 'Valid' : 'Invalid'});
OUTPUT:
Email: [email protected] is Valid Email: apple-email is Invalid