0

i was wondering if how can you accept any extensions for the email address. and not by specific domain name extension.

For example

foo@surrey.ac.uk <-- needed to work and surrey is the extension

foo@survey.ac.uk <-- needed to work and survey is the extension

this is my code. this is working but the problem is that if you added a extension for the email ac.uk, its not accepting it. for example, if i registered an email foo@ac.uk, it works, but if you tried adding an extension for the email like foo@surrey.ac.uk or foo@survey.ac.uk, its not accepting it. i want it to have any extensions added to be working. THANK YOU GUYS!

//for custom email
jQuery.validator.addMethod('customemail', function(value, element) {
    var s=value;
    var split = s.split('@');
    var regex = /^([a-zA-Z0-9_.+-])+$/;
    var s2="ac.uk";                  //The split array is the domain excluding the @
    var optionalValue = this.optional(element); //This is how other methods in alternativeMethods.js Validator handle this.

    //Debugging - This is useful to see visually what is happening
    //alert(split[0]);  // Shows the inputted username i.e chris or smokey
    //alert(split[1]);  // Shows the inputted domain
    //alert(regex.test(split[0]));  //Shows unfilled inputs problem or bad characters, true if good, false if bad
    //alert(s2 == split[1]);**  // Shows if the inputted domain matches variable s2, if it does we get a true

    if (optionalValue) {
        return optionalValue;
    }
    if(regex.test(split[0]) && (s2 == split[1]))  // has to be == not equals
    {
        return true;
    }
    else
    {
        return false;
    }
}, 'Please specify a Verified Email');

Some person reported this query as a duplicate from this post Email validation using jQuery

But my question is different since i needed a approved domain extension. not a general email validation

  • Hello @Jim i think this is not a duplicate question. i'm asking a specific question that required a different domain added extension. im also not that good at java script as i'm still a student. but if you could help me with my query i would be glad. –  Sep 17 '17 at 17:34
  • Apologies, at first glance it appeared to be the same. It is a partial match. – Jim Sep 18 '17 at 17:01

1 Answers1

0

First off, you should use the validation regular expression in the duplicate post entry - it is much better than your since you include a .+ which means match any 1 or more of any character - which would allow a lot of invalid emails through. You should validate the entire email, not just one part or the other.

As for matching the domain, since you want to include subdomains, use a regular expression to match either the domain exactly or ending with the domain but with a period ahead of it (any@ac.uk is valid, any@surrey.ac.uk is valid, but any@surreyac.uk or any@google.com is not a match).

JSFiddle Example

//for custom email
jQuery.validator.addMethod('customemail', function(value, element) {
  var s=value;
  var split = s.split('@');
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var s2=/(^|\.)ac.uk$/; // Regex to ensure domain is either exactly "ac.uk" or ends with ".ac.uk"

  //Debugging - This is useful to see visually what is happening
  //alert(split[0]);  // Shows the inputted username i.e chris or smokey
  //alert(split[1]);  // Shows the inputted domain
  //alert(regex.test(split[0]));  //Shows unfilled inputs problem or bad characters, true if good, false if bad
  //alert(s2 == split[1]);**  // Shows if the inputted domain matches variable s2, if it does we get a true

  // Ensure entire email is valid and that it is or ends with the required domain
  if(regex.test(s) && (s2.test(split[1])))
  {
    return true;
  }
  else
  {
    return false;
  }
}, 'Please specify a Verified Email')
Jim
  • 3,210
  • 2
  • 17
  • 23
  • it worked! thank you very much. i copied everything from the code that you have and it worked. :) –  Sep 19 '17 at 12:33