What do i need to add... something small

Discussion in 'JavaScript' started by oo7ml, Jun 19, 2007.

  1. #1
    My email validation is as follows

    apos=value.indexOf("@")
    dotpos=value.lastIndexOf(".")
    if (apos<1||dotpos-apos<2) 
    {alert(alerttxt);return false}
    else {return true}
    
    HTML:
    but if i enter an address as example@mydomain. it accepts it because there is nothing to say that there has to be something after the .

    Do you know how to ammend this, or so you have a better script than this, thanks
     
    oo7ml, Jun 19, 2007 IP
  2. nanolab

    nanolab Active Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You need to compare length of email string and dotpos
     
    nanolab, Jun 21, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    IMO regex is the way to go ..... my pattern might be a bit flaky, if you find some email it gives unexpected results on do tell......

    
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    function checkEmail( address )
    {
    	if( !address.match( /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ) )
    	{
    		document.getElementById( 'theMessage' ).innerHTML = '<font color=red>Email address is <b>NOT</b> valid</font>';
    	}
    	else
    	{
    		document.getElementById( 'theMessage' ).innerHTML = '<font color=blue>Email address is valid</font>';
    	}
    }
    </script>
    </head>
    
    <body>
    <div id="theMessage"></div>
    <form action="" method="post" name="theForm">
    	<input type="text" name="email" />
    	<input type="button" onclick="return checkEmail( document.theForm.email.value )" value="Check Email"/>
    </form>
    </body>
    </html>
    
    HTML:
     
    krakjoe, Jun 21, 2007 IP