SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: mail validation

  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post mail validation

    now i made my form validation for mail using this code



    //mail // mail validation
    var y;
    y=document.getElementById('emal').value
    if((y.length==0) || (y.charAt(0)==' '))
    {
    alert(mailerror);
    document.getElementById('email').style.color="#FF0100";
    document.getElementById('emal').focus();
    return false;
    break;
    }
    var p
    p=document.getElementById('emal').value;
    if((p.length!=0))
    {
    document.getElementById('email').style.color="#000000";
    }
    if (document.getElementById('emal').value.indexOf('@',0)==-1 ||
    document.getElementById('emal').value.indexOf('@',0)== 0 ||
    document.getElementById('emal').value.indexOf('.',0)==-1 ||
    document.getElementById('emal').value.indexOf('.',0)== 0)
    {
    alert(invalidmail);
    document.getElementById('email').style.color="#FF0100";
    document.getElementById('emal').focus();
    return false;
    break;
    }



    my problem is that if i enter the mail like this

    abc.sss@xyz it works correctly coz it search for the dot and @ without knowing its position

    so i want to validate that the dot is after the sign @ And if it is before it dont display an error msg

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    var emailAddress = 'john.smith.22@example.com';
    if(emailAddress.match(/^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/)) {
        alert('VALID');
    } else {
        alert('INVALID')
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look if u tried to write abc.abc@yahoo it works correctly
    but i need it to be abcabc@yahoo.com
    so the dot in the first one is before @
    but i need it to be after
    i want to detect if the dot is before @ neglectit and continue bcoz the dot must be after the @
    thnx in advance

  4. #4
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could u plz explain this code?and i will try to fix it

  5. #5
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code I posted uses a regular expression - which is a bit like a pattern of characters. If the emailAddress string matches the pattern then we can say it's valid.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  6. #6
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here it's my code in (funtionc.js)
    function fd()
    {
    do {

    //mail

    var y;
    y=document.getElementById('emal').value
    if((y.length==0) || (y.charAt(0)==' '))
    {
    alert(mailerror);
    document.getElementById('email').style.color="#FF0100";
    document.getElementById('emal').focus();
    return false;
    break;
    }
    var p
    p=document.getElementById('emal').value;
    if((p.length!=0))
    {
    document.getElementById('email').style.color="#8B8B8B";
    }
    if (document.getElementById('emal').value.indexOf('@',0)==-1 ||
    document.getElementById('emal').value.indexOf('@',0)== 0 ||
    document.getElementById('emal').value.indexOf('.',0)==-1 ||
    document.getElementById('emal').value.indexOf('.',0)== 0)
    {
    alert(invalidmail);
    document.getElementById('email').style.color="#FF0100";
    document.getElementById('emal').focus();
    return false;
    break;
    }



    }while(0)
    }





    and this is mypage.php


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" language="JavaScript1.2" src="library/stmenu.js"></script>
    <script src="library/prototype.js" type="text/javascript"></script>
    <script src="library/scriptaculous.js" type="text/javascript"></script>
    <script type="text/javascript" src="library/functions.js"></script>





    <script language="javascript">
    {

    var mailerror='Please enter your E-mail';
    var invalidmail='Please enter a correct e-mail address';

    }
    </script>




    </head>


    <body>

    <table width="80&#37;" border="0" cellpadding="0" cellspacing="0" class="res" >
    <form id="form3" name="form3" method="post" action="index.php" onSubmit="return fd();">


    <tr>
    <td width="25%" align="left" valign="middle" >
    <label for="emal" id="email"> E-Mail * </label></td>
    <td width="5%" align="center" valign="middle" >/td>
    <td width="55%" height="25" align="left" valign="middle" ><input name="emal" type="text" id="emal" size="44" /></td>
    </tr>



    <tr>
    <td align="left" valign="middle" >&nbsp;</td>
    <td align="center" valign="middle" >&nbsp;</td>
    <td align="center" valign="middle">

    <div align="center">
    <input name="15" type="submit" class="submit" id="15" value="Submit" />
    <input type="hidden" name="subm" id="subm">
    </div></td>
    </tr>
    </form>
    </table>

    </body>
    </html>



    i tried to put the code u sent to me inside my form validation but it did not work
    could you plz help me
    thnx in advance

  7. #7
    SitePoint Enthusiast Chris Webb's Avatar
    Join Date
    Oct 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris Webb | twitter/tgnwg
    Visit TheGoodNamesWereGone.com for
    free Javascript goodness, help with your Website and more


  8. #8
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why is this guy creating multiple threads on the same problem!?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  9. #9
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry Mr/JimmyP for interruption


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
  •