SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: submit a form

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    submit a form

    I made a form with javascript to check form validation. This part works fine.

    Once all the necessary fields are filled, click the submit button. I want to add the javascript here that only allows the button to be pressed once. How can I do that?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if at the start off your validation you add
    Code:
    document.getElementById('submit').disabled = true;
    this will disable the button from use and then if the validation fails re-enable the button by setting it to false

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <script type="text/javascript">
    function checkform(form) {
    	if (form.chkb.checked == false){
    		alert("Please accept the Terms and conditions");
    
    		return false;
    	}
    	
    	if (form.name.value == ""){
    		alert("Please enter card holder name");
    
    		return false;
    	}
    	if (form.card_number.value == ""){
    		alert("Please enter card number");
    
    		return false;
    	}
    	
    	return true;
            document.getElementById('accept').disabled = true;
    }
    </script>
    
    <form action="...">
    ...
    <input type="submit" name="accept" id="accept" value="Submit">
    </form>
    Is it right?

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its should be, i change the errors as well so only shows one error box will all error messages.

    Code:
    <script type="text/javascript"> 
    function checkform(form) { 
    	document.getElementById('submit').disabled = true; 
    	var msg = ''
    	if (form.chkb.checked == false){ 
    		msg += "Please accept the Terms and conditions\n"; 
    	};
    	if (form.name.value == ""){ 
    		msg += "Please enter card holder name\n"; 
    	} ;
    	if (form.card_number.value == ""){ 
    		msg += "Please enter card number\n"; 
    	} ;
    	
    	if(msg){
    		alert(msg);
    		document.getElementById('submit').disabled = false; 
    		return false;
    	}else{
    		return true;
    	};
    }
    </script>
    
    <form name="test" onsubmit="return checkform(this);">
    ...
    <input type="submit" id="submit" />
    </form>

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!


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
  •