• 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.
  • Posts

    • I'm really happy that you're looking to apply for a role in writing articles for Neowin. We could do with more, regardless, so everyone is happy and has a story to read when ever they login to the site. https://www.neowin.net/contact/apply I look forward to reading what you have to say in the near future in one of the published articles here on Neowin. You can do it. You got this.
    • Uranus releases about 15% more energy than it receives from the Sun, according to two new papers published in the journal Monthly Notices of the Royal Astronomical Society and the journal Geophysical Research Letters. The amount of heat a planet exudes could be an indication of its age: the less heat released relative to the heat absorbed from the Sun, the older the planet is. Uranus stood out from the other planets because it appeared to give off as much heat as it received, implying it had none of its own. This puzzled scientists. Some hypothesized that perhaps the planet is much older than all the others and has cooled off completely. Others proposed that a giant collision — the same one that may have knocked the planet on its side—blasted out all of Uranus’ heat. But none of these hypotheses satisfied scientists, motivating them to solve Uranus’ cold case. https://www.sci.news/space/warmer-uranus-14079.html  
    • The reality is that it's much, much cheaper to have a single code base than rewrite and maintain everything in native frameworks for each platform. It's just sad that the most reliable technology stack for it is so heavy on resources (literally 3 times the RAM, according to the screenshots).
    • Trump Logic:  If people are talking about football team names... they won't be talking about Epstein! 🤣
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      Michael Scrip
      203
    3. 3
      ATLien_0
      197
    4. 4
      Xenon
      136
    5. 5
      +FloatingFatMan
      116
  • Tell a friend

    Love Neowin? Tell a friend!