Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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');     
share|improve this question
    
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. –  Sparky Mar 16 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. –  Smokey Mar 16 at 18:06

2 Answers 2

up vote 1 down vote accepted

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');
share|improve this answer
    
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. –  Sparky Mar 16 at 19:03
    
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. –  Smokey Mar 16 at 19:20
1  
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. –  Sparky Mar 16 at 19:27
    
attaboy keep working –  Arjun Chaudhary Mar 16 at 23:15
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;
share|improve this answer
1  
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 –  Smokey Mar 16 at 17:38
    
which return type –  Arjun Chaudhary Mar 16 at 17:40
    
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 Mar 16 at 17:43
    
@Smokey, You should always return a boolean true or false from the function in the .addMethod() method. –  Sparky Mar 16 at 17:49
    
I've amended my code but I can't get it to work. –  Smokey Mar 16 at 18:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.