SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Need help editing this email validation script

    Years ago I found this script and put it on my website to validate email addresses. But now there are email addresses like q.com that can't register on my site because the script is looking for at least two digits before the ".com" Can someone point out the line I need to change to fix this problem? I looked through it and it's all Greek to me.

    Thanks!


    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  
      if(!d) d=document; 
      if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
      }
      if(!(x=d[n])&&d.all) 
        x=d.all[n]; 
      for (i=0;!x&&i<d.forms.length;i++) 
        x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
        x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) 
        x=d.getElementById(n); 
      return x;
    }
    
    function MM_validateForm() { //v4.0
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (i=0; i<(args.length-2); i+=3) { 
        test=args[i+2];
        val=MM_findObj(args[i]);
        if (val) { 
          nm=val.name; 
          if ((val=val.value)!="") {
            if (test.indexOf('isEmail')!=-1) { 
             if(junkjunk=val.match(/(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/)) {
                    // valid email, do nothing
                  } else {
                    errors+='- '+args[i+1]+' must contain a valid e-mail address.\n';
                  }
    
            } 
            else if (test!='R') { 
              num = parseFloat(val);
              if (isNaN(val)) 
                errors+='- '+args[i+1]+' must contain a number.\n';
              if (test.indexOf('inRange') != -1) { 
                p=test.indexOf(':');
                min=test.substring(8,p); 
                max=test.substring(p+1);
                if (num<min || max<num)
                  errors+='- '+args[i+1]+' must contain a number between '+min+' and '+max+'.\n';
              }
            }
          } 
          else if (test.charAt(0) == 'R') {
            errors += '- '+args[i+1]+' is a required field\n'; 
          }
        }
      } 
      if (errors) alert('Please address the following:\n'+errors);
      document.MM_returnValue = (errors == '');
    }
    //-->
    </script>
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Those dreamweaver scripts are notoriously horrible.

    It will also fail if the bit before "@" is only one character long. The only bit that checks for the email being valid is this:

    Code:
    if(junkjunk=val.match(/(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/)) {
      // valid email, do nothing
      }
    Validating an email address properly is a difficult task, and with the arrival of internationalised domain names, lots of email validation scripts will break. I would just do this:
    Code:
    if (/^[^ @]+@[^ @]+\.[a-zA-Z]{2,6}$/.test(email_string)) {
      not invalid
    }
    This has the disadvantage of being too broad, so it is simple enough to get it to accept an invalid email address (like ...@....com). But it's broad enough that it reject an email that might actually be valid. Even with the strictest validation scripts, it's probably possible to trick them into accepting an invalid address. And of course it doesn't stop people putting in fake but valid addresses, so it's not worth making this very strict anyway. If there are other characters you wish to reject, you can put them between the square brackets (but after the ^ in the brackets). At the moment, it only rejects spaces and "@" before the "@" and between the "@" and the last dot.

  3. #3
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if(junkjunk=val.match(/(\w+[\w|\.|-]*\w+)(@[\w|\.|-]*\w+\.\w{2,4})/)) {

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Dogfang, what does your tweak do in this case?

    Thanks
    Convert your dollars into silver coins. www.convert2silver.com

  5. #5
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a slight simplification of the validation.
    As Raffles points out, there is very little advantage in strictly validating email addresses with JavaScript. If a user makes a typo you probably won't catch the error and spammers will have JavaScript disabled.
    Server-side validation is where the work is done.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Raffles,

    So do I replace this existing line of code:

    if(junkjunk=val.match(/(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/)) {


    With this one?

    if (/^[^ @]+@[^ @]+\.[a-zA-Z]{2,6}$/.test(email_string))

    The reason I still need to have some sort of validation in this script is because it checks other things, like making sure they provided a city, state, etc. So please let me know.

    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's right. That test should work. But of course you should test it and see. If I were you I would get rid of those MM_ scripts (and ditch dreamweaver altogether).


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
  •