my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes Allow only one Email in text box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Allow only one Email in text box" Watch "Allow only one Email in text box" New topic
Author

Allow only one Email in text box

Preethi Natarajan
Greenhorn

Joined: Apr 19, 2013
Posts: 6
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

Joined: Jun 08, 2010
Posts: 384

Hi Preethi,

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


http://plainoldjavaobject.blogspot.in
Preethi Natarajan
Greenhorn

Joined: Apr 19, 2013
Posts: 6
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

Joined: Jan 08, 2009
Posts: 1473
    
    6

What is the problem are you facing with this code?


Swastik
Preethi Natarajan
Greenhorn

Joined: Apr 19, 2013
Posts: 6
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

Joined: Jan 08, 2009
Posts: 1473
    
    6

How are you identifying that it has multiple email ids? There must be some separator between two e.g space?
Preethi Natarajan
Greenhorn

Joined: Apr 19, 2013
Posts: 6
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

Joined: Jan 08, 2009
Posts: 1473
    
    6

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

Joined: Apr 19, 2013
Posts: 6
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

Joined: Jun 08, 2010
Posts: 384

Hi Preethi,

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

cheers :-)
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

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

Joined: Apr 19, 2013
Posts: 6
Great job Swastik, Its working fine ... Thank you very much.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

You are welcome.
Carsten Eilers
author
Greenhorn

Joined: Mar 28, 2013
Posts: 13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allow only one Email in text box