Link to home
Start Free TrialLog in
Avatar of poldings
poldingsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of Om Prakash
Om Prakash
Flag of India image

//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");
        }
      
<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();
  }
}
Avatar of poldings

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of JohnSixkiller
JohnSixkiller
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect. Works a treat.