Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form submission domain restriction

    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.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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.

  • #4
    New Coder
    Join Date
    Jun 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    thank you for fast response all of you, I tried danman's method and worked


    thanks.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    Jun 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx glenngv nice work


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •