form validation problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meenakshia
    New Member
    • Jun 2008
    • 33

    form validation problems

    hi forum
    i have been trying to use a way to validate my form but i was unable to use it properly
    pls rectify my mistakes.

    i m using input type as a button and calling a function to save the form data.
    now i want to check if all fields are entered properly before submitting the form

    the javascript function i m using is

    Code:
    <script language="JavaScript">
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("txtddate", "txtspid");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("Delivery Date", "Sales Person Name");
    	// 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";
    				}
    			}
    		}
    	}
    
    	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    </script>
    and form details are
    Code:
    <form name="outputForm1" >
    <td>Delivery Date</td>
    <td><input type="text" name="txtddate" tabindex="1"  size="20"  ></td>
    <td>Sales Person Name</td>
    <td><select name="txtspid" tabindex="2">
    	<option selected>Pls. Choose</option>
    	<option value="1">Hemant</option>
    	<option value="2">Rajan</option>
    	<option value="3">Ranjan Kumar</option>
    	<option value="4">Sanjay Parwani</option>
    	<option value="5">Santosh</option>
    	<option value="6">Rajesh</option>
        </select>
    </td>
    <td><input type="button" value="Save" name="btnSave" tabindex="21" onclick = "SaveForm();"></td>
    if you want to see the save form function,pls let me know i will post it seperately.

    i m not able to put the function before the saveform function runs
    pls advice how i should go ahead:)

    smile always:)
    anand
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Call the validation function onsubmit:
    [code=html]<form ... onsubmit="retur n formCheck()">[/code]What does saveForm() do?

    Comment

    • meenakshia
      New Member
      • Jun 2008
      • 33

      #3
      hi thanks for ur reply
      well actually i have posted only a part of the whole code
      it is too big
      and there are around 10 input fields for which the user has to input the data in
      and saveform function saves that data in ms access file(the database)
      my problem was that i was not able to use both saveform and form validator which is really important for the working of the whole form
      i will try and get back to u if it works fine:)
      smile always:)
      anand

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Does SaveForm() save to a local MS Access file or does it submit to the server?

        You can call SaveForm() from within the form validation function if it passes validation.

        Comment

        • meenakshia
          New Member
          • Jun 2008
          • 33

          #5
          hi yes it saves the data on local machine.there is no server and yes that is what i m trying to do without success
          i m trying to put saveform() inside the validation function but unable to run it:(

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            OK, make the save button into a submit button and remove the onclick. Add the onsubmit if you haven't already done so. Now in the formCheck() function where you would return true, call SaveForm() instead. To prevent form submission, you should also return false at the end of SaveForm().

            Comment

            • meenakshia
              New Member
              • Jun 2008
              • 33

              #7
              hi
              thanks a lot
              yes problem is solved:)
              thanke a ton:)
              smile always
              anand

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                No problem, you're welcome :)

                Comment

                Working...