...

View Full Version : Form submission domain restriction



jacven
06-23-2004, 12:34 AM
Hi, I have a web form with some fields (name, last name, company, email and so on) we want to only allow users to have in their email address, domains such as: @hotmail.com not other email provider will be allowed, we're looking for some javascript alert that validate that form.

Thanks.

Willy Duitt
06-23-2004, 01:16 AM
Try this:


<script type="text/javascript">
<!--//
function validate(form){
var goodEmail = /^[\w-]+(@hotmail\.com)$/.test(form.email.value);
if(!goodEmail){
alert('Please enter a valid hotmail email address.');
form.email.value = '';
form.email.focus();
return false;
} return true;
}
//-->
</script>
</head>

<body>
<form method="post" action="" onsubmit="return validate(this)">
Email address: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>

danman
06-23-2004, 02:50 AM
Willy's way was the most efficient and streamlined method, but this way will work, too. This way may not be as compact or neat, but it does the job and may be more reader-friendly and easier to update.



<head>
<script type="text/javascript">
domains=new Array("hotmail.com","aol.com","yahoo.com") //add all valid hosts to this array
function validateEmail()
{
em=document.the_form.email.value;
if(em.indexOf("@")==-1) {alert("Invaild email"); return false;}
else
{
dom=em.split("@")[1]
validDomain=false;
for(i=0;i<domains.length;i++)
{
if(dom==domains[i]) validDomain=true;
}
if(validDomain==false){alert("Invalid email host"); return false;}
else{return true;}
}
}
</script>
</head>
<body>
<form name="the_form" action="" onsubmit="return validateEmail()">
<input type="text" size="30" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>


I'm certainly no Javascript expert, but I think this will work just as well.

jacven
06-23-2004, 03:27 AM
thank you for fast response all of you, I tried danman's method and worked
:)

thanks.

glenngv
06-23-2004, 04:07 AM
Why not simply this?

Email address: <input type="text" name="email" title="Enter hotmail user id" />@hotmail.com

Then in the server-side, append the @hotmail.com in the entered user id.

jacven
06-23-2004, 04:09 AM
thx glenngv nice work :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum