Jump to content

Can't find out why script isn't working properly.


svdb

Recommended Posts

Hi, I have this script for registration form validation. It has three functions: - to check if the emailadress is valid, this function is initiated onblur- to check password lenght and whether or not the password control field matches the first input- to double check for the things above plus to check if there are any empty mandatory fields when the submit button is clicked. The first two work fine, the third works for all fields except the password lenght and match control. I don't understand why because I exactly used the same code in the first two functions and they work fine. Here is the code, hope someone can tell me what's going wrong.

	function checkPass(){		var pass1 = document.getElementById('password');		var pass2 = document.getElementById('passwordcontrol');		var message = document.getElementById('confirmMessage');		var goodColor = "#AFC100";		var badColor = "#f20000";		if (pass1.value.length < 4){		pass2.style.borderColor = badColor;		message.style.color = badColor;		message.innerHTML = "Wachtwoord is te kort";		}		else if(pass1.value == pass2.value){		pass2.style.borderColor = goodColor;		message.style.color = goodColor;		message.innerHTML = "Wachtwoord is correct";		}		else{		pass2.style.borderColor = badColor;		message.style.color = badColor;		message.innerHTML = "Wachtwoord komt niet overeen";		}	}	  	function validateEmail()		{		var mail = document.getElementById('mail');		var message = document.getElementById('mailMessage');		var badColor = "#f20000";		var goodColor = "#AFC100";		var x=document.forms["registerform"]["mail"].value;		var atpos=x.indexOf("@");		var dotpos=x.lastIndexOf(".");		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)			{			mail.style.borderColor = badColor;			message.style.color = badColor;			message.innerHTML = "Ongeldig e-mailadres";			}		else {			mail.style.borderColor = goodColor;			message.innerHTML = ""		}	}  	function validateForm()	{		var valid = true;		var badColor = "#f20000";		var u=document.forms["registerform"]["username"].value;		var p=document.forms["registerform"]["password"].value;		var pc=document.forms["registerform"]["passwordcontrol"].value;		var m=document.forms["registerform"]["mail"].value;		var pstc=document.forms["registerform"]["postcode"].value;		var pts=document.forms["registerform"]["plaats"].value;		var s=document.forms["registerform"]["straat"].value;		var nr=document.forms["registerform"]["nummer"].value;		var atpos=document.getElementById('mail').value.indexOf("@");		var dotpos=document.getElementById('mail').value.lastIndexOf(".");		var pass1 = document.getElementById('password');		var pass2 = document.getElementById('passwordcontrol');		var message = document.getElementById('confirmMessage');		var message = document.getElementById('warnMessage');		if (u==null || u=="")			{			document.getElementById('username').style.borderColor = badColor;			valid = false;			}		if (p==null || p=="")			{			document.getElementById('password').style.borderColor = badColor;			valid = false;			}		if (pc==null || pc=="")			{			document.getElementById('passwordcontrol').style.borderColor = badColor;			valid = false;			}		if (m==null || m=="")			{			document.getElementById('mail').style.borderColor = badColor;			valid = false;			}		if (pstc==null || pstc=="")			{			document.getElementById('postcode').style.borderColor = badColor;			valid = false;			}		if (pts==null || pts=="")			{			document.getElementById('plaats').style.borderColor = badColor;			valid = false;			}		if (s==null || s=="")			{			document.getElementById('straat').style.borderColor = badColor;			valid = false;			}		if (nr==null || nr=="")			{			document.getElementById('nummer').style.borderColor = badColor;			valid = false;			}		if (!document.registerform.akkoord.checked)			{			document.getElementById('voorwaarden').style.border = "medium solid #f20000";			valid = false;			}// the two if's below don't work	  if (pass1.value.length < 4 )			{			valid = false;			}		if (pass1.value != pass2.value)			{			valid = false;			}// 		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)			{			valid = false;  			}		if (!valid)			{			window.scrollTo(0,0);			message.style.color = badColor;			message.innerHTML = "U heeft niet alle verplichte velden juist ingevuld.";			return false;			}		else			{			return true;			}	}

Any other tips on the script are welcome, i'm new to javascript.

Edited by svdb
Link to comment
Share on other sites

can you be more specific about what is actually happening? Are you checking for errors in the console? What debugging have you done to find out where the issue is? You should use console.log('some text or variables here') to trace the functions and find out what the values of the variables are and when so you can understand what each line of your code is doing. don't just assume what is going, verify for yourself what is actually going on.

Link to comment
Share on other sites

Just from the start I found something, not an error, but won't do what you want I don't think.

if (pass1.value.length < 4){			    pass2.style.borderColor = badColor;			    message.style.color = badColor;			    message.in

Shouldn't it be:pass1.style.borderColor = badColor; ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...