Jump to content

Fair Few Problems


morrisjohnny

Recommended Posts

Hi, I'm having problems checking to see if

  • passw & passw2 Match
  • tos is checked

if the passw's dn't match then it needs to display and error (like the e-mail verfication)and the same with tos being uncheckedI have made an effort but failed. could someone please point me in the right direction of edit my code so it works?Here's my code (i'm using php aswell so that will explain all the \"'s

<script type=\"text/javascript\">function validate_email(field,alerttxt){with (field){apos=value.indexOf(\"@\")dotpos=value.lastIndexOf(\".\")if (apos<1||dotpos-apos<2)  {alert(alerttxt);return false}else {return true}}}function isChecked(){	if (document.tos.checked)	{		return true	}	return false}function matchPass(){	if ((document.thisform.passw.value)==(document.thisform.passw2.value))	{		return true	}	return false}function validate_form(thisform){with (thisform){	if (validate_email(email,\"Not a valid e-mail address!\")==false)	  {email.focus();return false}	  {	  	if (isChecked(tos,\"You Have to agree to the Terms Of Service\")==false)	  	{email.focus();return false}		{			if (matchPass(passw==passw2,\"Sorry Your Password Do Not Match\")==false)		  	{email.focus();return false}		}	  }	}}</script></head><body> <table align=\"center\" border=\"0\">  <tr><td colspan=\"2\" align=\"center\">You May Register Below<form action=\"\" method=\"post\" onsubmit=\"return validate_form(this);\"></td></tr>   <tr><td>Username</td><td><input type=\"text\" name=\"username\" value=\"Username\" /></td></tr>   <tr><td>Password</td><td><input type=\"password\" name=\"passw\" value=\"Password\" /></td></tr>   <tr><td>Verfi Password</td><td><input type=\"password\" name=\"passw2\" value=\"Password2\" /></td></tr>   <tr><td>E-Mail</td><td><input type=\"text\" name=\"email\" value=\"email\" /></td></tr>   <tr><td>Tick To Agree with the <a href=\"TOS.html\">TOS</a></td><td><input type=\"checkbox\" name=\"tos\" value=\"agree\" /></td></tr>   <tr><td colspan=\"2\" align=\"center\"><input type=\"Submit\" name=\"Submit\" value=\"Register\" /></form></td></tr>   <tr><td colspan=\"2\" align=\"center\"><a href=\"index.php\">Home</a></td></tr> </table></body>

Link to comment
Share on other sites

The first thing I notice is that in the javascript, you reference the password inputs as "password" and "password2" but in the HTML they are named "passw" and "passw2".

Link to comment
Share on other sites

OK. It looks like you may have a few too many curly braces in your validate_form function:

function validate_form(thisform){	with (thisform)	{		if (validate_email(email,\"Not a valid e-mail address!\")==false)		{			email.focus();			return false		}		{		if (isChecked(tos,\"You Have to agree to the Terms Of Service\")==false)		{			email.focus();			return false		}		{		if (matchPass(passw==passw2,\"Sorry Your Password Do Not Match\")==false)		{			email.focus();			return false		}	}}}}

Also, there appears to be a problem with the way you are calling the matchPass function. As the matchPass function is defined, it doesn't accept any parameters but you are passing two parameters: passw==passw2 which will always equate to false (because two different elements can never be equivalent) and a string "Sorry Your Password Do Not Match".If you are going to call a function to validate the password, you might consider calling it like this:

with(thisform){	if(matchPass(passw.value, passw2.value) == false)	{		// validation failed	}}

And then rebuild that function to be something like this:

function matchPass(passw1, passw2){	if(passw1 != passw2)	{		// passwords do not match		return false;	}	else	{		return true;	}}

Link to comment
Share on other sites

Sorry about my late reply i've had so much school work and that to do lately, My Problem still continuesIf i add in the email validation part first it works fine but as soon as i add in anything else it doesn't work. something must be wrong with the way i am laying it out?

with (thisform){	if (validate_email(email,\"Not a valid e-mail address!\")==false)	  {email.focus();return false}	if (isChecked(tos,\"You Have to agree to the Terms Of Service\")==false)	  	{email.focus();return false}	function matchPass(passw1, passw2)		{		if(passw1 != passw2)	    	{			// passwords do not match			return false;	    	}	    else   			{       		return true;	    	}		}}

Link to comment
Share on other sites

This is your code without the backslashes and table code.

<form action="" method="post" onsubmit="return validate_form(this);">Username <input type="text" name="username" value="Username" />Password <input type="password" name="passw" value="Password" />Verfi Password <input type="password" name="passw2" value="Password2" />E-Mail <input type="text" name="email" value="email" />Tick To Agree with the <a href="TOS.html">TOS</a><input type="checkbox" name="tos" value="agree" /><input type="Submit" name="Submit" value="Register" /></form>

I noticed that you didn't name your form. If you don't care about validation, I would go:

<form name="register" ... >

or if you want it valid:

<form id="register" ... >

Now you can start using name or id when running your JavaScript. Here's an example you can use: http://oneuse.awardspace.com/form_validation.phpAlternatively, you can use the following code to check if your passwords are not the same:

if (register.pw1.value != register.pw2.value)	{	  alert("Your passwords do not match.");	  return false;	}

Where pw1 and pw2 are your password name inputs and register is your form name.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...