www.webdeveloper.com
Results 1 to 6 of 6

Thread: onsubmit returning false and form gets submitted anyway

  1. #1
    Join Date
    Feb 2008
    Posts
    17

    onsubmit returning false and form gets submitted anyway

    Hi,

    Code:
    function testForm(form){
    
    	var reden = "";
    	
    	reden += controleerTekstVelden(form);
    	reden += controleerEmail(form.mail);
    	
    	if( reden != "" ){
    		toonError(reden);
    		return false;
    	}else{
    		return true;
    	}
    }
    
    function controleerTekstVelden(form){
    
    	var error = "";
    	
    	if ( form.naam.value.length == 0 ){
    		form.naam.style.background = 'Yellow';
    		error += "Naam vergeten invullen \n";
    	} else if ( form.voornaam.value.length == 0 ) {
    		form.voornaam.style.background = 'Yellow';
    		error += "Voornaam vergeten invullen \n";
    	} else if ( form.telefoon.value.length == 0 ) {
    		form.telefoon.style.background = 'Yellow';
    		error += "Telefoonnummer vergeten invullen \n";
    	} else if ( form.bericht.value.length == 0 ) {
    		form.bericht.style.background = 'Yellow';
    		error += "Bericht vergeten invullen \n";
    	}
    	
    	return error;
    	
    }
    
    function controleerEmail(veld){
    	
    	var error = "";
    	
    	with (field)
    	{
    		apos=value.indexOf("@");
    		dotpos=value.lastIndexOf(".");
    		
    		if (apos<1||dotpos-apos<2){ 
    			form.mail.style.background = 'Yellow';
    			error = "Incorrect email adres."
    			return error;
    		}else{
    			return error;
    		}
    	}
    
    }
    This is my js to validate my form ( i'v let out my function to actualy display the error message on the page since it's not relevant ) and it called this way:

    HTML Code:
    <form onsubmit="return testForm(this);" name="mail" action="index.php?page=c&sb=2" method="post">
    Now my problem is the following: eventough my function returns false the form gets submitted anyway...

    I'v tried few things, went over the code line by line but can't solve it.
    I was hoping someone here could.

    Thx

  2. #2
    Join Date
    Aug 2006
    Location
    England
    Posts
    665
    Quote Originally Posted by Dzan
    I'v tried few things, went over the code line by line but can't solve it.
    I suspect I know what the problem is, but you need to confirm that there are no messages showing in the error console.
    FireFox > Tools > Error console

    Translation?
    Scripterlative.com

    PM = 'PayPal Mode'

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259

    Lightbulb

    Also, change your 'error = ""' code to
    a logical true/false for the IF to work.
    For example: if (error) { ...

    or leave as is and test as
    if (error == '') { ...
    or
    if (error != "") { ...

    but return a logical value for the
    onsubmit="return ...
    to work correctly

  4. #4
    Join Date
    Feb 2008
    Posts
    17
    I checked the console and I do get an error:

    Fout: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIWebNavigation.loadURI]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://global/content/viewSource.js :: viewSource :: line 152" data: no]



    @JMRKER

    I do check like you say I think, look:

    Code:
    if( reden != "" ){
    		toonError(reden);
    		return false;
    	}else{
    		return true;
    	}
    And I return false or true, those are logics right?

  5. #5
    Join Date
    Aug 2006
    Location
    England
    Posts
    665
    Quote Originally Posted by Dzan
    I checked the console and I do get an error:

    Fout: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIWebNavigation.loadURI]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: chrome://global/content/viewSource.js :: viewSource :: line 152" data: no]


    Code:
    function controleerEmail(veld){
    	
    	var error = "";
    	
    	with (field)
    	{
    Where do you define field?
    Scripterlative.com

    PM = 'PayPal Mode'

  6. #6
    Join Date
    Feb 2008
    Posts
    17
    Guess javascript didn't know field was english for the dutch 'veld'

    Sorry for opening this stupid thread I overlooked it

    Thx alot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles