Can someone please advise what I may be missing? How can I not only make it better, but this does not seem to be working, although 1 domain instance has worked for some time, I tried adding to it:

if(document.form1.email.value.length > 0) {
			var theEmail = document.form1.email;
			var at = theEmail.value.indexOf('@');
			var period = theEmail.value.lastIndexOf('.');
			var space = theEmail.value.indexOf(' ');
			var length = theEmail.value.length - 1;
			var mlcom = theEmail.value.indexOf('ml.com');
			var MLCOM = theEmail.value.indexOf('ML.COM');
			if ((at < 1) ||                    
				(period <= at+1) ||          
				(period == length ) ||          
				(space  != -1) ||				
				(length <=period + 1))			                   
			{  
				 alert("Please enter a valid e-mail address.");
				 document.form1.email.select();
				 return false;
			} else if ((mlcom == -1) && (MLCOM == -1)){
				alert ("Please enter an email address containing the phrase 'ml.com'.");
	 			document.form1.email.select();
	 			return false;
			}
		}

Recommended Answers

All 6 Replies

This

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">

        var tst = 'name@M1.com'
        var okd = ['m1.com','whatever.net']
        for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
        var aLst = emailRE.exec(tst)
        if (!aLst) {
            alert(tst + ' is not valid')
        } else {
            var sLst = aLst[1].toLowerCase()
            for (var i = 0; i < okd.length; i++) {
                if (sLst == okd[i]) {
                    alert(aLst[1] + ' is allowed');
                    break
                }
            }
            if (i==okd.length) alert(aLst[1] + ' is not allowed')
        }

    </script>
    <title></title>
  </head>
  <body>
  </body>
</html>

first tests the format of the string against a regexp for email addresses, and if it is acceptable then the domain.tld is checked against your list of 'ok' domains.

Note: the pattern I use is good but not ultra-precise, and it won't work for non-western scripts.

THANK YOU VERY MUCH!!

However I can't get it past the BREAK, when valid email address with approved domain are entered.

//email validation       
		if (document.form1.email.value.length > 0) {

		var tst = document.form1.email.value;
        var okd = ['baml.com','ml.com','ust.com']
        for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
        var aLst = emailRE.exec(tst)
        if (!aLst) {
            alert(tst + ' is not a valid e-mail')
        } else {
            var sLst = aLst[1].toLowerCase()
            for (var i = 0; i < okd.length; i++) {
                if (sLst == okd[i]) {
                    alert(aLst[1] + ' is allowed');
                    break
                }
            }
            if (i == okd.length) alert(aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.')
            	 
            return false;
        }	
	}

Here is where I am at now, but after valid e-mail still can't get to move on to next form validation step:

if (document.form1.email.value.length > 0) {

		var tst = document.form1.email.value;
        var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
        for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
        var aLst = emailRE.exec(tst)
        if (!aLst) {
            alert(tst + ' is not a valid e-mail')
        } else {
            var sLst = aLst[1].toLowerCase()
            for (var i = 0; i < okd.length; i++) {
                if (sLst == okd[i]) {
                //    alert(aLst[1] + ' is allowed');-->
                //   break
             
                }
            }
            if (i == okd.length) alert(aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.')
            	 	 
            document.form1.email.select();
            return false;
        }	
	}

is this really SO tough??

Thank You for the jump start FXM!! Your code was the core solution.
I added a Try/Catch to jump the error message when e-mail/domain were valid.

Here is final code fyi...

   
		if (document.form1.email.value.length > 0) {

		    var tst = document.form1.email.value;
		    var okd = ['amer.com', 'bama.com', 'man.com', 'mall.com', 'dust.com', 'rust.com']
		    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }

		    try {
		        var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
		        var aLst = emailRE.exec(tst)

		        if (!aLst)
		            throw (tst + ' is not a valid e-mail');

		        // isValidDomain will be changed to 'true' only if it matches an item in the array 
		        var isValidDomain = false;

		        var sLst = aLst[1].toLowerCase()
		        for (var i = 0; i < okd.length; i++) {
		            if (sLst == okd[i]) {
		                isValidDomain = true;
		                // We break here because a match has been found - no need to compare against the other domain names. 
		                // break - exits code from running on down to next item on page
		            }
		        }

		        if (!isValidDomain)
		            throw (aLst[1] + ' is not allowed.  Please enter an email address with an authorized domain.');

		        // If execution reaches here, you know it passed both tests! 
		     //   return true; - was not needed, stops code from running on page

		    }
		    catch (err) {

		        // This code block runs whenever an error occurs 

		        alert(err);
		        document.form1.email.select();
		        return false;
		    }
		}

Try?!

Packaging the code as a function is fine, but it could be so much simpler.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">

    var okd = ['amer.com', 'bama.com', 'man.com', 'mall.com', 'dust.com', 'rust.com']
//  for (var i = 0; i < okd.length; i++) {
//      okd[i] == okd[i].toLowerCase()
//  }
    var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/

    function ckEmail(tst) {  
	    var aLst = emailRE.exec(tst)
	    if (!aLst) return 'A valid e-mail address is requred';
	    var sLst = aLst[1].toLowerCase()
	    for (var i = 0; i < okd.length; i++) {
	        if (sLst == okd[i]) {
	            return true
	        }
	    }
	    return aLst[1] + ' is not an allowed domain.';
    }

    var ckValid = ckEmail(prompt('Enter your email address'))

    if (ckValid === true) {
        alert(ckValid)  // placeholder for process validated
    } else {
        alert(ckValid)  // placeholder for show error message
    }

    </script>
    <title></title>
  </head>
  <body>
  </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.