0

I need to make a new method for jQuery Validator and don't know where to start.

I would like it check that the email entered includes: '@specificdomain.com'.

But that it is also the very last part of the input. For example @specificdomain.comChris would not do.

<script type="text/javascript">
    jQuery.validator.addMethod("mustinclude", function(value, element) {
        return this.optional(element) || value == ?
        }, "must include @specificdomain.com at the end of the text input");

    $(document).ready(function(){ .....

So far I've only come across value == value.match(), hence this is where I've got stuck.

Cheers Chris

        jQuery.validator.addMethod('matchDomain', function(value, element) {
        var s=value;
        var split = s.split('@');
        var regex = /^([a-zA-Z0-9_.+-])+$/;
        var s2="@allcoles.com";
        var optionalValue = this.optional(element);

        if (optionalValue) {
            return optionalValue;
            }
        if(regex.test(split[0]) && s2.equals(split[1]))
            {
            return true;
            }
        else
            {
            return false;
            }
        }, 'Please specify a @allcoles.com email');     
2
  • There are literally many dozens of working .addMethod() examples inside the additional-methods.js file. Looking at those rules would be a great place to learn how it's done. Commented Mar 16, 2014 at 17:43
  • Thank you, I couldn't find the method or anything close to it but it did help me find what I needed to do with the element. Commented Mar 16, 2014 at 18:06

2 Answers 2

1

The following worked for me:

jQuery.validator.addMethod('matchDomain', function(value, element) {
        var s=value;
        var split = s.split('@');
        var regex = /^([a-zA-Z0-9_.+-])+$/;
        **var s2="allcoles.com";**                  //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 @allcoles.com email');
Sign up to request clarification or add additional context in comments.

3 Comments

Please edit to point out where this code differs from your OP. When writing questions and answers, keep in mind that SO is also a learning tool for future readers.
I added my answer to help others, I know its a learning tool. I have however edited my post with bold to point out the 3 changes. Apologies for not doing this.
You have the right idea and please don't think I'm being picky, but a brief verbal description (or inline comments) would be far better than adding asterisks inside code. I'm trying to make the point that explaining something is better than expecting somebody to do a side-by-side comparison.
1
var s="[email protected]";  OR  var s=value;
var split = s.split('@');
var regex = /^([a-zA-Z0-9_.+-])/;
var s2="@specificdomain.com";

if(regex.test(split[0]) &&  s2 == split[1])

       return true;
else
         return false;

5 Comments

Hi thank you for the help; I added it in a addMethod. I don't think I understand the return types correctly as its not working correctly. Cheers Chris
Its a JQuery Validator addMethod jqueryvalidation.org/jQuery.validator.addMethod, ive confused myself as im not sure if im returning a Boolean false or an element. I amended my original post to include your code
@Smokey, You should always return a boolean true or false from the function in the .addMethod() method.
s2.equals(split[1])). I guess this is an array, should my var s2 be specificdomain.com and not @specificdomain.com?
Almost there equals not being == was the problem, and the s2 split did not need an @. But I would not have got this working without your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.