Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Allow only one Email in text box

 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to allow only one email ID in text box using java script validation with HTML.

for example: abc@gmail.com multiple email should not allow.
I'm new to Javascript. If any one have idea then please share it.

Thanks,
Preethi
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preethi,

Welcome to Ranch.
Please share what have by yourself so far :-)

 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code:

<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
</head>

<body>
<form name="myForm" action="" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>

last index .com after should not allow any other email or char
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the problem are you facing with this code?
 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my code, Its allowing multiple email ids, I wold like to allow only one email ID, how to implement that validation.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you identifying that it has multiple email ids? There must be some separator between two e.g space?
 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example abc@gmail.com bcd@gmail.com is allowing in my case, I want to allow only one email id should allow,
How to implement that function..
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already have the index position of last dot. Make sure you don't find any special character or space after that, I believe this should be the logic.
 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Swastik,

How to validate last index .com after, If I type any char or space then I want to show alert message ie, onkeyup function how to implement that function

My code
var email = document.getElementById("mdEmail").value;
var at=email.indexOf("@");
var dot=email.lastIndexOf(".");
var gmail_email=email.toLowerCase().indexOf("gmail.com");
if(gmail_email== -1)
{
alert("Please provide a valid gmail e-mail address for MD");
return false;
}
if (at<1 || dot<at+2 || dot+2>=email.length)
{
alert("Please provide a valid gmail e-mail address for MD");
return false;
}
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preethi,

Just make sure there's no two @ in the string.

cheers :-)
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but it won't validate aa@gmail.com1 or ab@gmail.comm, right?

Prrethi,

You may wish to give a look on the following function


 
Preethi Natarajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great job Swastik, Its working fine ... Thank you very much.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Carsten Eilers
author
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi Natarajan wrote:Hi

I want to allow only one email ID in text box using java script validation with HTML.

for example: abc@gmail.com multiple email should not allow.
I'm new to Javascript. If any one have idea then please share it.

Thanks,
Preethi


Hi Preethi,

only to be sure you don't make a beginners error: You must test the input on the server, too. An attacker can simply disable your JavaScript-code or use a proxy and send multiple emails in one parameter!

Kind regards
Carsten
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic