Validate Email is from Specific Domain

We have a php form that collects some basic information ...

- email
- firstname
- lastname
- company

This signup form is specific to one client so we want to block everyone else from signing up.

Examples of correct email addresses ...

dave@change-domain.co.uk
sue@change-domain.co.uk
sales@change-domain.co.uk
anything@change-domain.co.uk

Any address that does not use '@change-domain.co.uk' is incorrect and should throw up an error ...

"sorry - that email is not valid"

We want the check to be made before form submission - so, after the user enters their email address and tabs to the next field.

Thanks.
poldingsAsked:
Who is Participating?
 
JohnSixkillerCommented:
Yes, I see. There is a problem with Sender.focus(); Replace it with: window.setTimeout(function(){Sender.focus();},10);
0
 
Om PrakashCommented:
//Add following javascript validation
        var str= document.form1.emailfield.value;
        var a2 = str.toLowerCase();
        if(a2.indexOf("@change-domain.co.uk") == -1)
        {
              alert("sorry - that email is not valid");
        }
      
0
 
JohnSixkillerCommented:
<input type='text' name='email' id='email' onchange='validateEmail(this)'>

function validateEmail(Sender){
  var validSuffix = "change-domain.co.uk";
  var emailSuffix = Sender.value.substring(Sender.value.indexOf("@")+1);

  if(validSuffix != emailSuffix){
      alert("sorry - that email is not valid");
      Sender.value = "";
      Sender.focus();
  }
}
0
 
poldingsAuthor Commented:
JohnSixkiller:
That works great. Just one thing to make it a bit more user friendly pls ...

How can I adapt it so the focus is back on the 'email' field after the alert has closed?
0
 
poldingsAuthor Commented:
Perfect. Works a treat.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.