Regular Expressions – validating the Email

How to validate an email address with following rules using regular expressions?

  • There should one @ sign in the email address.
  • The email should start with a letter and can include letters and numbers and underscore symbol  _ . This constitutes the left hand side of the email from the @ sign.
  •  Right hand side of the @ sign should be the domain name. This should include the period . and left hand side of the period should be alpha numeric and right hand side should be letters.
  • Lower case and upper case is supported.

Lets get started on how to validate for above rules using regular expressions.


This means the reg expression has started.


This part means the first letter should be a letter. Either lower case a-z or upper case A-Z.


After that We check for alpha numeric characters and underscore character.


Then we check for the @ sign.


Then we check for the domain name alpha numeric characters and – dash sign.


Then we check for the period in the domain name.


Then we check for the domain extension letters.


Last part is to mark the end of the reg expression.

Hope this help you understand regular expressions a bit more. I have added the regular expression for javascript. But this will be similar for other languages as well. Please share how to validate email in other languages using regex below in the comments.


Regular Expression – Javascript- To check filename to see if alpha numeric characters

Ok here is a quick regular expression to check if a file name only contains alpha numeric characters  also we need to allow the period sign.


This allows numbers, letters and the period symbol.

The full code,

var filename = fullfilepath.replace(/^.*[\\\/]/, ''); // this is to get only the file name, file upload field gives the full path

 if (!filename.match(/^[0-9a-zA-Z\.]*$/)) {
 alert('File name should only containg letters and numbers, please rename image file and upload.');