1

I have a form with an email field.
As part of the validation I would like to exclude certain domains ( eg gmail,yahoo etc )

I have found the following function which should search for substrings which I believe is a good choice to search my email strings in: jquery/javascript check string for multiple substrings:

function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
        if(str.indexOf(arguments[i]) > -1){
            return true;
        }
    }    
    return false;
}    

isSubStringPresent('myemail', 'gmail', 'yahoo', ...)

I am confused on how to integrate this function with the jquery validator plugin.

Please see below:

I have the following following below my form:

$(document).ready(function(){ 
    $.validator.addMethod(
                    "wrong_domain",
                    THE isSubStringPresent FUNCTION SHOULD BE INSERTED HERE SOMEHOW...                    
                    },
                    'Not an acceptable email' );

    $.validator.classRuleSettings.wrong_domain = { wrong_domain: true};    

$('#registration-form').validate({
        rules: {
           FirstName: {
            required: true,
          },

           Email: {
            required: true,
            email: true,
            wrong_domain: true
          },

Can someone show me how to do this?

1 Answer 1

1

Try using apply to pass in multiple excluded domains to the validation function:

var excludes = ['myemail', 'gmail', 'yahoo'];
$.validator.addMethod(
    "wrong_domain",
    function(value, element) { 
        // take a shallow copy of excludes
        var myArguments = excludes.slice(0);
        // add value to the front of the arguments
        myArguments.unshift(value);
        // use myArguments as the arguments array for isSubStringPresent
        return !isSubStringPresent.apply(this, myArguments);
    },
    'Not an acceptable email' );

Here's an example Fiddle. This will validate the input after every keypress event.

Sign up to request clarification or add additional context in comments.

Comments

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.