SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript and Mozilla

    I'm getting a weird error in Mozilla which I can't seem to find an answer to. Basically, I'm doing some simple form validation before submitting the form. If it fails I want it to stop the submittal process. The code I've got works fine for IE but in Mozilla it flashes the error on-screen with an alert statement but then proceeds to submit the form anyway.

    the code is

    Code:
    for (var loop = 0; loop < fields_to_check.length; loop++)
    			{
    				if (document.forms[form_to_check].elements[fields_to_check[loop]].value == '')
    				{
    					alert ('You must enter '+field_warnings[loop]+' !');
    					document.forms[form_to_check].elements[fields_to_check[loop]].focus();
    					event.returnValue=false;
    					return false;
    				}
    }
    It's the event.returnValue=false statement that stops the form submission but Mozilla seems to ignore it and searching on Google hasn't turned up any answers. All ideas are welcome

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you set a flag -- isOk2Send = true; -- then switch it when validation fails, and submit the form through the function -- if (isOk2Send) formObj.submit()

    See the Validating Forms script/tutorial at my site ( GrassBlade )

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no JS guru, but I think your problem is that IE and Moz have different event models, and you're using the IE one.

    Any chance you can show us ALL the code?

  4. #4
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's part of a large PHP page but here's the pertinent bits

    Code:
    <script language="JavaScript">
    		function validateRegistrationForm()
    		{
    			var fields_to_check = Array('username','email','password');
    			var field_warnings = Array('a username','an email address','a password');
    			var form_to_check = 'user_registration_form';
    			
    			// loop through the fields we want to check
    			for (var loop = 0; loop < fields_to_check.length; loop++)
    			{
    				if (document.forms[form_to_check].elements[fields_to_check[loop]].value == '')
    				{
    					alert ('You must enter '+field_warnings[loop]+' !');
    					document.forms[form_to_check].elements[fields_to_check[loop]].focus();
    					event.returnValue=false;
    					return false;
    				}
    }
    }
    </script>
    form is straightforward. Ignore the PHP escape characters

    Code:
    <form name=\"user_registration_form\" action=\"".$PHP_SELF."\" method=\"post\" onSubmit=\"validateRegistrationForm()\">
    
    loads of fields
    
    </form>

  5. #5
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that your problem has to do with the return value of the validate function and how it is handled within the onsubmit event handler in the form tag.

    I would suggest the following changes:
    Code:
    <form ... onsubmit="return validateRegistrationForm()"> 
    Code:
     function validateRegistrationForm() {
      var rc = true; // until proven otherwise.
      ....
      for (var loop = 0; loop < fields_to_check.length; loop++) {
    	if (document.forms[form_to_check].elements[fields_to_check[loop]].value == '') { 
    	...
    	// event.returnValue=false;
    	rc = false;
    	}
      }
      ....
      return rc;
    }
    Kiffin
    Your average future-famous kind of guy...

  6. #6
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stroke of genius kgish, or at least it is to my humble JavaScript skills. Adding that return to the <form> HTML has done the trick. I didn't even need the extra variable rc

  7. #7
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well even the most humble javascript gurus need a stroke of genius once in awhile. Even if it comes from a random passerby like me.

    Glad I could help!
    Kiffin
    Your average future-famous kind of guy...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •