2 Replies - 331 Views - Last Post: 30 May 2012 - 07:09 AM

#1 ibnmvungi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 20-April 12

error on returning the email validation to the form

Posted 28 May 2012 - 12:17 AM

hallo every one, i have this error,when a user enter unfinished email address like ghf@ the java script function doesn't return an erro. here is the form and the javasript function.please help me.
<html>
<head>
<script>
function checkfrm(frm)
{
  if(frm.username.value=='')
  {
    alert("username is required");
   frm.username.focus();
   return false;
   }
    

  else if  (frm.email.value=='')

  {
   alert("email address is required");
   frm.email.focus();
   return false;
   }
   

  else if( document.forms["frm"]["email"].value.indexOf("@")<1 || dotpos<atpos+2 || document.forms["frm"]["email"].value.lastIndexOf(".")+2>=document.forms["frm"]["email"].length ){

      alert('Invalid Email Address');
      return false;
   
   }
 
  else
      {
	     return true;
	      }
  }
  
  
</script>

</head>
<body >
<td valign="top" bgcolor="#FFFFCC" align="center"><br/><br/><br/>
<form action='?action=forgot-passwordck' method="POST" name="frm" onsubmit="return checkfrm(this)" >
<table border='0' cellspacing='0' cellpadding='0' align=center>
 <tr bgcolor='#f1f1f1' > <td colspan='2' align='center'><font face='verdana, arial, helvetica' size='2' align='center'>&nbsp;Forgot Password ?<BR>Enter your email address and Username</font></td> </tr>
 <tr id='cat'>
  <tr bgcolor='#ffffff'> <td><font face='verdana, arial, helvetica' size='2' align='center'>  &nbsp;Username  &nbsp; &nbsp;
</font></td> <td  align='center'><font face='verdana, arial, helvetica' size='2' >
<input type ='text' class='bginput' name="username" ></font></td></tr>
<tr bgcolor='#f1f1f1'> <td><font face='verdana, arial, helvetica' size='2' align='center'>  &nbsp;Email  &nbsp; &nbsp;
</font></td> <td  align='center'><font face='verdana, arial, helvetica' size='2' >
<input type ='text' class='bginput' name="email" ></font></td></tr>



<tr bgcolor='#ffffff'> <td  colspan='2' align='center'><font face='verdana, arial, helvetica' size='2' align='center'>  
<input type="submit" value="Submit" name="submit"> <input type="reset" value="Reset">
</font></td> </tr>

<tr> <td bgcolor='#f1f1f1' ><font face="verdana, arial, helvetica" size='2' align="center"> &nbsp;<a href="?action=login" style="color: #2D3954; font-size: 11px; ">Login</a></font></td> <td bgcolor='#f1f1f1' align='center'><font face='verdana, arial, helvetica' size='2' ><a href="?action=registration" style="color: #2D3954; font-size: 11px; ">New Member Sign UP</a></font></td></tr>



</table></center></form>

<center>
</td>

</body>
<html>







Is This A Good Question/Topic? 0
  • +

Replies To: error on returning the email validation to the form

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3044
  • View blog
  • Posts: 4,556
  • Joined: 08-June 10

Re: error on returning the email validation to the form

Posted 28 May 2012 - 01:25 AM

Hey.

Let me guess. You copied that last else if clause from somewhere (W3Schools?) but don't really understand it? - You should always read through code like that carefully, make sure you understand it, and make sure you are implementing it correctly. Otherwise, regardless of it's advertised purpose, it may well not work at all (a lot of code posted online is utter rubbish), or you may be incorrectly applying it.

To break it down for you, this is what that else if clause is trying to do. It's split into three separate conditions, all of which must be false if your email is to be considered valid.

  • The first part simply makes sure there is an @ char in the email.

  • The second part is meant to make sure the position of the last . in the email comes at least two chars after the @ char.

  • The last part is meant to make sure the last . is at least 2 characters away from the end of the string, making the top-level domain name at least that long.


Your mistake was in part 2. You just do dotpos<atpos+2, but those variables haven't been defined anywhere. The source where you copied that from did so, I'm guessing, before the IF clause to save having to repeat the indexOf calls. Since you didn't do that you'll have to repeat the calls on the form element to get the value. (Like you do with the other two parts of the statement.)

If you avoided defining those variables yourself to be able to use else if clauses, rather than a series of if clauses, there is really no point. All the checks either return, effectively ending the function execution, or move on to the next if. So you may as well do it as a series of single if clauses.

Also, just because W3Schools uses the document.forms["form"]["elem"] syntax doesn't mean you have to too. You actually pass in the form as a parameter to the function (frm), so you really should be using that instead.

This post has been edited by Atli: 28 May 2012 - 01:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 ibnmvungi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 20-April 12

Re: error on returning the email validation to the form

Posted 30 May 2012 - 07:09 AM

okey thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1