Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to restrict a list of domain name in Email validation using JS with demo

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.46k
    Comment on it

    If you want to validate your email field with a list of domain name and normal email validation, then we can do that with the help of following code:

    HTML:

    <div class="form">
    	<input id="emailAdd" type="text" onkeyup="emailVal()"/>
    	<button id="btnSub" type="button" disabled="disabled">Validate</button>
    </div>

    Javascript:

    var domain = [];
    var countDomain = 0;
    
    var request = $.ajax({
      url: "email.json",
      dataType: "html"
    });
     
    request.done(function( msg ) {
        domain = JSON.parse(msg);
        domain = (domain)
        countDomain = domain.length;
    });
     
    request.fail(function( jqXHR, textStatus ) {
      alert( "Request failed: " + textStatus );
    });
    
    
    function emailVal(){
    	var email = $("#emailAdd").val();
    	validateEmail(email);
    }
    
    function validateEmail(email) {
    	
        var re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
        var status = false;
        if (re.test(email)) {
        	for(i = 0;i < countDomain; i++){
        		if (email.indexOf(domain[i].domain, email.length - (domain[i].domain).length) > 0) {
    	            status = false;
    	            document.getElementById('btnSub').disabled = true;
    	            break;
    	        } else {
    	        	status = true;
    	        	document.getElementById('btnSub').disabled = false;
    	        }            
    			
    	        
        	}
            
        } else {
    	    document.getElementById('btnSub').disabled = true;
        }
        
    	
    }

    JSON:

    [{
    	"domain": "0-mail.com"
    }, {
    	"domain": "0815.ru"
    }, {
    	"domain": "0clickemail.com"
    }, {
    	"domain": "0wnd.net"
    }, {
    	"domain": "0wnd.org"
    }, {
    	"domain": "10minutemail.com"
    }, {
    	"domain": "20minutemail.com"
    }, {
    	"domain": "2prong.com"
    }, {
    	"domain": "30minutemail.com"
    }, {
    	"domain": "3d-painting.com"
    }, {
    	"domain": "4warding.com"
    }, {
    	"domain": "4warding.net"
    }, {
    	"domain": "4warding.org"
    }, {
    	"domain": "60minutemail.com"
    }, {
    	"domain": "675hosting.com"
    }, {
    	"domain": "675hosting.net"
    }, {
    	"domain": "675hosting.org"
    }, {
    	"domain": "6url.com"
    }, {
    	"domain": "75hosting.com"
    }, {
    	"domain": "75hosting.net"
    }, {
    	"domain": "75hosting.org"
    }, {
    	"domain": "7tags.com"
    }]
    

    You can take the domain name through server or from your local file in the folder as well but you have to update the list if you want to add domain name.
    You can download the demo attached.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: