Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I need help with email validation.

    Hi, can anyone please check my code and tell me what's wrong with my email validation. It used to be working when i had the following code:

    if(document.myForm.email.value==""){
    document.getElementById('email1').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    New Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Courtesy Of Big Moosie</title>


    <script language="JavaScript">
    <!--

    function SymError()
    {
    return true;
    }

    window.onerror = SymError;

    var SymRealWinOpen = window.open;

    function SymWinOpen(url, name, attributes)
    {
    return (new Object());
    }

    window.open = SymWinOpen;

    //-->
    </script>

    <script type="text/javascript">
    function validate(myForm){

    var validForm=true;

    if(document.myForm.name.value==""){
    document.getElementById('name1').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|c oop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=myForm.email.value.match(emailPat);

    if (matchArray==null) {
    document.getElementById('email1').innerHTML="<i><span>Email address seems incorrect</span></i>";
    validForm=false;
    }

    var user=matchArray[1];
    var domain=matchArray[2];

    for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
    document.getElementById('email1').innerHTML="<i><span>This username contains invalid characters</span></i>";
    validForm=false;
    }
    }

    for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
    document.getElementById('email1').innerHTML="<i><span>This domain name contains invalid characters</span></i>";
    validForm=false;
    }
    }

    if (user.match(userPat)==null) {
    document.getElementById('email1').innerHTML="<i><span>The username doesn't seem to be valid.</span></i>";
    validForm=false;
    }

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    document.getElementById('email1').innerHTML="<i><span>Destination IP address is invalid!</span></i>";
    validForm=false;
    }
    }
    validForm=true;
    }

    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;

    for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
    document.getElementById('email1').innerHTML="<i><span>The domain name does not seem to be valid</span></i>";
    validForm=false;
    }
    }

    if (checkTLD && domArr[domArr.length-1].length!=2 &&
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
    document.getElementById('email1').innerHTML="<i><span>The address must end in a well-known domain or two letter " + "country.</span></i>";
    validForm=false;
    }

    if (len<2) {
    document.getElementById('email1').innerHTML="<i><span>This address is missing a hostname</span></i>";
    validForm=false;
    }

    if(document.myForm.address.value==""){
    document.getElementById('address1').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    if(document.myForm.city.value=="" || document.myForm.zip.value==""){
    document.getElementById('cityandzip').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    if(document.myForm.phone.value==""){
    document.getElementById('phone1').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    if(document.myForm.cardnumber.value==""){
    document.getElementById('cardnumber1').innerHTML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    if(document.myForm.month.options.selectedIndex==0 || document.myForm.year.options.selectedIndex==0){
    document.getElementById('expirationdate1').innerHT ML="<i><span>Incorrect</span></i>";
    validForm=false;
    }

    return validForm;

    }
    </script>

    <style>
    table{font-weight:bold}
    span{color:red}
    select { background: #FFFF80; color:black; }
    body,td,th {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 7.5pt;
    }
    </style>



    </head>

    <body onload="var SymTmpWinOpen = window.open; window.open = SymWinOpen; document.myForm.name.focus();; window.open = SymTmpWinOpen;">
    <form name="myForm" onsubmit="return validate()">
    <fieldset>
    <legend>Please enter the following information</legend>

    <table width="100%" border="0">
    <tr>
    <td width="15%" bgcolor="#FFFF80"><strong>* Name:</strong></td>
    <td width="20%" bgcolor="#FFFF80"><input name="name" type="text" id="name" size="30" onfocus="document.getElementById('name1').innerHTML='';"></td>
    <td width="32%" id="name1">&nbsp;</td>
    <td width="32%">&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Email:</strong></td>
    <td bgcolor="#FFFF80"><input name="email" type="text" id="email" size="30" onfocus="document.getElementById('email1').innerHTML='';"></td>
    <td id="email1">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Address:</strong></td>
    <td bgcolor="#FFFF80"><input name="address" type="text" id="address" size="30" onfocus="document.getElementById('address1').innerHTML='';"></td>
    <td id="address1">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>&nbsp;&nbsp;&nbsp;Address1(optional):</strong></td>
    <td bgcolor="#FFFF80"><input name="address1" type="text" id="address1" size="30"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* City/State/Zip(11111):</strong></td>
    <td nowrap bgcolor="#FFFF80"> <input name="city" type="text" id="city" size="10" onfocus="document.getElementById('cityandzip').innerHTML='' ;">
    /
    <select name="fl" id="fl">
    <option>AK</option>
    <option>AL</option>
    <option>AR</option>
    <option>AZ</option>
    <option>CA</option>
    <option>CO</option>
    <option>CT</option>
    <option>DC</option>
    <option>DE</option>
    <option>FL</option>
    <option>GA</option>
    <option>HI</option>
    <option>IA</option>
    <option>ID</option>
    <option>IL</option>
    <option>IN</option>
    <option>KS</option>
    <option>KY</option>
    <option>LA</option>
    <option>MA</option>
    <option>MD</option>
    <option>ME</option>
    <option>MI</option>
    <option>MN</option>
    <option>MO</option>
    <option>MS</option>
    <option>MT</option>
    <option>NC</option>
    <option>ND</option>
    <option>NE</option>
    <option>NH</option>
    <option>NJ</option>
    <option>NM</option>
    <option>NV</option>
    <option>NY</option>
    <option>OH</option>
    <option>OK</option>
    <option>OR</option>
    <option>PE</option>
    <option>RI</option>
    <option>SC</option>
    <option>SD</option>
    <option>TN</option>
    <option>TX</option>
    <option>UT</option>
    <option>VA</option>
    <option>VT</option>
    <option>WA</option>
    <option>WI</option>
    <option>WV</option>
    <option>WY</option>
    </select>
    /
    <input name="zip" type="text" id="zip" size="3" onfocus="document.getElementById('cityandzip').innerHTML='' ;"></td>
    <td id="cityandzip">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Phone:</strong></td>
    <td bgcolor="#FFFF80"><input name="phone" type="text" id="phone" onfocus="document.getElementById('phone1').innerHTML='';"></td>
    <td id="phone1">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Card Number:</strong></td>
    <td bgcolor="#FFFF80"><input name="cardnumber" type="text" id="cardnumber" maxlength="16" onfocus="document.getElementById('cardnumber1').innerHTML=' ';"></td>
    <td id="cardnumber1">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Card Type:</strong></td>
    <td bgcolor="#FFFF80"><select name="cardtype" id="cardtype">
    <option>Visa</option>
    <option>Discover</option>
    <option>MasterCard</option>
    <option>AmericanExpress</option>
    </select></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#FFFF80"><strong>* Expiration Date:</strong></td>
    <td bgcolor="#FFFF80"><select name="month" id="month" onfocus="document.getElementById('expirationdate1').innerHT ML='';">
    <option>Month</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    </select>
    <select name="year" id="year" onfocus="document.getElementById('expirationdate1').innerHT ML='';">
    <option>Year</option>
    <option>2005</option>
    <option>2006</option>
    <option>2007</option>
    <option>2008</option>
    <option>2009</option>
    <option>2010</option>
    </select></td>
    <td id="expirationdate1">&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td ><input type="submit" name="Submit" value="submit" > <input type="reset" name="Reset" value="Reset"></td>

    </table>

    </fieldset>
    </form>
    </body>

    <script language="JavaScript">
    <!--

    window.open = SymRealWinOpen;

    //-->
    </script>

    </html>

    Thanks in advance

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    I think the problem is you need to have quotation marks around your regular expressions as they're meant to be strings (if I'm not mistaken)
    e.g.
    var emailPat="/^(.+)@(.+)$/";
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."


  •  

    Posting Permissions

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