Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML:

<form id="registerform3" method="post" name="registerform3" action="finish.html"  önsubmit="checkPass();my();">
<input type="submit">
</form>


I want to trigger two functions on onsubmit event. can it be possible? and if yes than how?

Javascript:

function my()
{ var pass1 = document.getElementById('setpassword');
    var pass2 = document.getElementById('confirm');

    var message = document.getElementById('message1');
	

    var goodColor = "#0C6";
    var badColor = "#FF9B37";
	 if(pass1.value.length>=8){
        
        pass1.style.backgroundColor = goodColor;
       message.style.color = goodColor;
	 
        message.innerHTML = "Password Accepted!";
	return true;
    }else {
   
        pass1.style.backgroundColor = badColor;
		
        message.style.color = badColor;
        message.innerHTML = "Minimum 8 characters!";
		return false;
    }
}
function checkPass()
{


    var pass1 = document.getElementById('setpassword');
    var pass2 = document.getElementById('confirm');
    
    var message = document.getElementById('message2');

   var goodColor = "#0C6";
    var badColor = "#FF9B37";
  
    if(pass1.value == pass2.value){
       
	   
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Matching!";
		return true;
		
		
    }else {
      
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Doesn't Matching!";
		return false;
	
	
    }
	
	
	
}
Posted
Updated 18-Mar-14 3:16am
v3

Event handlers in HTML (like onsubmit) are uing JavaScript code, so writing return something; will stop the execution flow.
If you want to execute multiple function you can do two things:
1. Create a function that calls upon all others (preferred IMHO)

HTML
<form ...  önsubmit="return(onSubmit());">


JavaScript
function onSubmit()
{
  var rv = checkPass();

  my();

  // other thing to do

  return(rv);
}


2. Write in event handler all the functions you want to call: onsubmit="chcekPass();my();"
 
Share this answer
 
v2
Comments
Krupal5 18-Mar-14 8:48am    
see edited
Kornfeld Eliyahu Peter 18-Mar-14 8:52am    
Your change is seem all right - that will execute both functions before posting the form. However if you want to use these function to stop the flow in certain cases, it's not good. It also not too goo as writing practice...
As I told, the best way is wrapping all your function into one and use it - see updated answer...
Krupal5 18-Mar-14 9:01am    
ya..I wanna stop the flow in function body on particular input.
Kornfeld Eliyahu Peter 18-Mar-14 9:04am    
So? Did you saw my updated solution?
Krupal5 18-Mar-14 9:13am    
m updating the question
XML
<form id="registerform3" method="post" name="registerform3" action="finish.html"  önsubmit="return checkPass() & my() & my2() & my3();">
<input type="submit">
</form>
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900