validate mail id using javascript [Resolved]

Posted by Shreedar under JavaScript on 9/28/2015 | Points: 10 | Views : 443 | Status : [Member] | Replies : 7
For name and password I implemented in javascript
<script language="javascript" type="text/javascript">
function ValidateForm() {
var name = document.getElementById("TextBox1").value;
var pwd = document.getElementById("TextBox2").value;
var email=document.getelementById("TextBox3").value;
if (name == "" && pwd == "") {
alert("Username and Password are Necessory!, Please enter and try again.");
}
I want to check valid email id.

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com



Responses

Posted by: Sheonarayan on: 9/28/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
4
Down

Resolved
Use below function and pass the email text box value. If this function returns true, the email id entered is valid otherwise invalid.

function ValidateEmail(mail) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(mail);
}

See the live demo here http://techfunda.com/Howto/javascript/781/email-validation .

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
2
Down

Resolved
Try this

<html>
<head>
<script>
function ValidateForm() {

var name = document.getElementById("TextBox1").value;
var pwd = document.getElementById("TextBox2").value;
var email=document.getElementById("TextBox3").value;
if (name == "" && pwd == "") {
alert("Username and Password are Necessory!, Please enter and try again.");
}

var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if(!re.test(email)){
alert("Invalid Email");
}
}
</script>
</head>
<body>
Name:<input type="text" id="TextBox1" /><br/>
Password:<input type="text" id="TextBox2" /><br/>
Email:<input type="text" id="TextBox3" /><br/>
<input type="button" id="btnCheck" value="Check" onClick="ValidateForm()"/>
</body>
</html>

The regular expression
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
3
Down
you can check this link too http://www.regular-expressions.info/email.html for RFC 2822 complaint regex. The official site for RFC 2822 is https://tools.ietf.org/html/rfc2822#section-3.4.1

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shreedar on: 9/28/2015 [Member] Starter | Points: 25

Up
1
Down
Sheo sir.
Rajnilari is giving good responses I agree.
But not faster than you.
Mr.Raj its a great thing to get compliment from Sheo sir.

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
How can I be faster than my GURU? (:

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 9/28/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Good job Rajnilari, you were faster than me!

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shreedar on: 9/28/2015 [Member] Starter | Points: 25

Up
0
Down
Raj well said :)

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response