SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Blocking words from a form field

    How do i check for certain words like "hotmail" "gmail" "yahoo" within an email field using Javascript?

    I have a form validation function working on my form but it currently only checks that "@." are included the code so far..

    Code:
    // test if valid email address, must have @ and .
    var checkEmail = "@.";
    var checkStr = theForm.email.value;
    var EmailValid = false;
    var EmailAt = false;
    var EmailPeriod = false;
    for (i = 0;  i < checkStr.length;  i++)
    {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkEmail.length;  j++)
    {
    if (ch == checkEmail.charAt(j) && ch == "@")
    EmailAt = true;
    if (ch == checkEmail.charAt(j) && ch == ".")
    EmailPeriod = true;
    	  if (EmailAt && EmailPeriod)
    		break;
    	  if (j == checkEmail.length)
    		break;
    	}
    	// if both the @ and . were in the string
    if (EmailAt && EmailPeriod)
    {
    		EmailValid = true;
    		break;
    	}
    }
    if (!EmailValid)
    {
    alert("Please enter a valid email address");
    theForm.email.focus();
    return (false);
    }

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be using regular expressions to validate e-mail addresses. The string methods of particular interest when working with regex are str.search, str.match, and str.replace. Which expression is "best" is a subject of endless debate. A number of good patterns for validating e-mail addresses can be found at http://regexlib.com/ .

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply...

    It doesn't work in IE please help!??

    Code:
    var x;
    var baddomains = new Array();
    baddomains[0] = /hotmail/i;
    baddomains[1] = /yahoo/i;
    baddomains[2] = /gmail/i;
    baddomains[3] = /googlemail/i;
    baddomains[4] = /tidalsoftware/i;
    baddomains[5] = /@bmc/i;
    baddomains[6] = /@uc4/i;
    baddomains[7] = /orsyp/i;
    baddomains[8] = /@ca.com/i;
    baddomains[9] = /appworx/i;
    baddomains[10] = /honico/i;
    baddomains[11] = /axway/i;
    baddomains[12] = /msn/i;
    baddomains[13] = /@live/i;
    baddomains[14] = /yahoo/i;
    
    for (x in baddomains) {
    	if (theForm.email.value.match(baddomains[x])) {
    		var domain = '"' + baddomains[x] + '"';
    		domain = domain.replace("/","");
    		domain = domain.replace("/i","");
    		alert(baddomains[x] + " email accounts are not allowed");
    		theForm.email.focus();
    		return (false);
    	}
    }
    Last edited by paul_83uk; Jul 2, 2008 at 06:52.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The above code is working fine in Firefox but isn't working at all in ie7 is there something about the way i have doen each array that ie wouldn't like?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •