• 0

Javascript Form Validation Problems with IE


Question

Hi,

I am trying to implement a simple form validation script on the forms for my PHP Program. So far I have got it working perfectly in Firefox and Opera. IE isn't working right though. It uses onChange to validate input as the user fills out the form. When using IE, I input my input in my "email" field and then try to move onto the "emailconf" field to confirm it, but IE won't let me change the field as it is "invalid". It is invalid at that point, but the user needs to change the field to the confirmation field to make it valid (as they have to match). Here is my code;

function check_email() {
	email = document.getElementById("email").value;
	email_len = document.getElementById("email").value.length;
	emailconf = document.getElementById("emailconf").value;
	emailconf_len = document.getElementById("emailconf").value.length;
	emailchk = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	if (email == "" || email_len > 101 || emailchk.test(email) != true || emailconf == "" || emailconf_len > 101 || emailchk.test(emailconf) != true || email != emailconf) {
		document.getElementById("inf_email").className = "error";
		return false;
	}
	if (email != "" && email_len < 101 && emailchk.test(email) == true && emailconf != "" && emailconf_len < 101 && emailchk.test(emailconf) == true && email == emailconf) {
		document.getElementById("inf_email").className = "";
		return true;
	}

}

Having all validations done at once like that is the best method I have found so far. Otherwise I find the verification becomes kind of buggy, and doesn't give correct validation. Do I need an extra bit of code to get IE to allow me to change the field? I have a very small amount of javascript experience so please be clear when explaining things.

Thanks

EDIT: Thanks to a friend, I have now managed to find the problem. When calling this function on the "onChange" call I had onChange="return check_email();". It seems that IE was reading this return, and locking focus on the field until it returned true. More than likely nothing wrong with IE, just my poor understanding of JS. Removing the return from the onChange has fixed the problem. I left the returns in for the functions themselves, as an onSubmit function calls them for checking that all fields are valid, and uses the return.

Edited by Fourjays

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.