Back to all resources
email validation
regular expression
jQuery
event handling

Validation - Email

By Jared Malan
This JavaScript code snippet includes a function validEmail that validates an email address using a regular expression. The function takes a single argument, email, and returns true if the email matches the regular expression, indicating it is a valid email format, or false otherwise.

The regular expression used in validEmail checks for a pattern where the email must start with one or more characters that are not special characters or whitespace, followed by an "@" symbol, and ending with one or more sequences of characters separated by dots. Each sequence must not start with special characters or whitespace and must be at least two characters long.

Additionally, the code snippet includes a jQuery event handler that triggers when the user leaves (blur event) the input field with #email-el. When the event is triggered, it retrieves the value of the input field, checks if it is a valid email using the validEmail function, and if not, a placeholder for error handling logic is provided where actual error handling code should be implemented.