Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate email address textbox using JavaScript

Tags:

javascript

I have a requirement to validate an email address entered when a user comes out from the textbox.
I have googled for this but I got form validation JScript; I don't want form validation. I want textbox validation.
I have written below JScript but "if email invalid it's not returning the same page".

 function validate(email) {              var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;             //var address = document.getElementById[email].value;             if (reg.test(email) == false)              {                 alert('Invalid Email Address');                 return (false);             }  } 
like image 274
Ramasani Indrashaker Avatar asked Oct 03 '11 13:10

Ramasani Indrashaker


People also ask

How do you check if a string is an email JavaScript?

const validateEmail = (email) => { return String(email) . toLowerCase() . match( /^(([^<>()[\]\\.,;:\s@"]+(\. [^<>()[\]\\.,;:\s@"]+)*)|(".

How do I validate an email address in HTML?

The best way to "validate" an email addresses is to simply have them type it twice and run a Regex check that gives a WARNING to the user that it doesn't look like a valid email address if it does not match the pattern, and asks the user to double check.


1 Answers

Assuming your regular expression is correct:

inside your script tags

function validateEmail(emailField){         var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;          if (reg.test(emailField.value) == false)          {             alert('Invalid Email Address');             return false;         }          return true;  } 

in your textfield:

<input type="text" onblur="validateEmail(this);" /> 
like image 185
Ben Avatar answered Sep 17 '22 12:09

Ben