JavaScript : email validation – w3resource

email validation

validating the email is a very important point when validating an html form. on this page we have discussed how to validate an email using javascript: an email is a string (a subset of ascii characters) separated into two parts by the @ symbol. a “personal_information” and a domain, ie [protected email] the length of the personal_information part can be up to 64 characters and the domain name can be up to 253 characters.

the personal_info part contains the following ascii characters.

  • English letters in uppercase (a-z) and lowercase (a-z).
  • digits (0-9).
  • characters! # $ % & ‘ * + – / = ? ^ _ ` { | } ~
  • character. (dot, dot, or dot) as long as it’s not the first or last character and doesn’t go one after the other.

The domain name part [for example, com, org, net, in, us, info] contains letters, digits, hyphens, and periods.

valid email id example

  • [protected email]
  • [protected email]
  • [protected email]

invalid email id example

  • mysite.ourearth.com [@ is not present]
  • [email protected] [ tld (top level domain) cannot start with a dot “.” ]
  • @you.me.net [ no character before @ ]
  • [email protected] [ “.b” is not a valid tld ]
  • [ email protected] [ tld cannot start with a dot “.” ]
  • [email protected] [ an email must not start with “.” ]
  • mysite()*@gmail.com [regex here only allows characters, digits, underscores, and hyphens]
  • [email protected] [double dots are not allowed ]

javascript code to validate an email id

To get a valid email id, we use a regular expression /^[a-za-z0-9.!#$%&’*+/=?^_`{|}~-][email protected] [a-za-z0-9-]+(?:.[a-za-z0-9-]+)*$/. according to http://tools.ietf.org/html/rfc3696#page-5 ! # $ % & ‘ * + – / = ? ^ ` . { | } ~ characters are legal in the local part of an email address, but in the regular expression above those characters are filtered out. you can modify or rewrite that regular expression.

See Also:  Comment créer un événement Google Agenda directement depuis Gmail ? - ZDNet

flowchart:

Flowchart : JavaScript - Email validation

Regular Expression Pattern

we are going to apply the previous javascript function in an html form.

html code

javascript code

css code

view javascript email validation in browser

rfc 2822 standard email validation

regular expression pattern(ref: https://bit.ly/33cv2vn):

view javascript email validation (rfc 2822) in browser

You can use the following email addresses to test such regular expression:

list of valid email addresses

  • [protected email]
  • [protected email]
  • [protected email]
  • [protected email]
  • [protected email]
  • [protected email][123.123.123.123]
  • “email”@example.com
  • [email protected email]
  • [protected email]
  • [protected email]
  • [protected email]
  • [protected email ]
  • [protected email]
  • [protected email]

list of rare valid email addresses

  • much.”moreunusual”@example.com
  • very.unusual.”@”[email protected]
  • very.”(),:; <>[]”.very.”[email protected]\ “very”[email protected]

file_download download the validation code from here.

another javascript validation:

  • check if not empty
  • check if there are all letters
  • check if there are all numbers
  • check if there are floats
  • checking letters and numbers
  • checking string length
  • email validation
  • date validation
  • a sample registration form
  • phone number validation
  • credit card no. validation
  • password validation
  • ip address validation

previous: javascript: html form – length restriction next: javascript: html form – date validation

Leave a Reply

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