Results 1 to 4 of 4

Thread: Form Validation-Multiple Functions executing onSubmit/onClick

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Form Validation-Multiple Functions executing onSubmit/onClick

    I have being playing around with the code posted in form effects for email validation and required fields from the DD sample code. Obviously both codes work but when I try to use both of the them together I cannot get them to execute simultaneously onSubmit or onClick. What can I do to get multiple functions to execute onSubmit/onClick?

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    What you could do, is have one function which fires, but that function can start the other functions.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script language="JavaScript">
    <!--
    
    /***********************************************
    * Required field(s) validation v1.10- By NavSurf
    * Visit Nav Surf at http://navsurf.com
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    function formCheck(formobj,email){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("FirstName", "LastName","myemail");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("First Name", "Last Name","Email");
    	// dialog message
    	var alertMsg = "Please complete the following fields:\n";
    
    	var l_Msg = alertMsg.length;
    
    	for (var i = 0; i < fieldRequired.length; i++){
    		var obj = formobj.elements[fieldRequired[i]];
    		if (obj){
    			switch(obj.type){
    			case "select-one":
    				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "select-multiple":
    				if (obj.selectedIndex == -1){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "text":
    			case "textarea":
    				if (obj.value == "" || obj.value == null){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			default:
    			}
    			if (obj.type == undefined){
    				var blnchecked = false;
    				for (var j = 0; j < obj.length; j++){
    					if (obj[j].checked){
    						blnchecked = true;
    					}
    				}
    				if (!blnchecked){
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    			}
    		}
    	}
        var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
        if (formobj[email]&&!emailfilter.test(formobj[email].value)){
    	  alertMsg += " - " + 'incorrect email address' + "\n";
        }
    	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    // -->
    </script>
    
    
    
    <!--SAMPLE FORM -------------------------------->
    <form name="formcheck" onsubmit="return formCheck(this,'myemail');">
    First Name: <input type=text name="FirstName" size="25"><br>
    Last Name: <input type=text name="LastName" size="25"><br>
    EMail: <input name="myemail" type="text" style="width: 270px"><br />
    <input type=submit value="Submit Form">
    </form>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi Vic

    That worked a treat. I appreciate it.

Similar Threads

  1. onSubmit dont want to clear form
    By vineet in forum JavaScript
    Replies: 2
    Last Post: 03-23-2009, 03:46 AM
  2. Replies: 6
    Last Post: 09-02-2008, 04:57 PM
  3. Onclick with two functions
    By devil_vin in forum JavaScript
    Replies: 1
    Last Post: 09-22-2007, 08:58 AM
  4. Replies: 0
    Last Post: 01-29-2007, 06:04 PM
  5. Multiple onSubmit Attributes
    By gold2040 in forum JavaScript
    Replies: 2
    Last Post: 05-29-2006, 07:58 AM

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
  •