Results 1 to 7 of 7

Thread: form validation not working:(

  1. #1
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default form validation not working:(

    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 check the attachment

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

    smile always
    anand

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    1. I've changed a bit in the following function and highlighted the changes.
    Code:
    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 == 0 || obj.options[obj.selectedIndex].text == "") {
    					alertMsg += " - " + fieldDescription[i] + "\n";
    				}
    				break;
    			case "select-multiple":
    				if (obj.selectedIndex == 0) {
    					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) {
    		SaveForm();
    		return true;
    	} else {
    		alert(alertMsg);
    		return false;
    	}
    }
    2. In the HTML markup changed the following part. Highlighted the changes in the following code:
    Code:
    <input type="button" value="Save" name="btnSave" tabindex="21" onclick = "formCheck(document.forms['outputForm1']);"> //Corrected here on 04-Sep-2008
    In other words you'll call the 'SaveForm' function after checking the form elements using 'formCheck' function (if everything is ok). But I personally think the date validation should be more strict. Now if a person can enter anything and make your validation pass, with a non-date value directly entering it in the text box instead of using the input calendar. I feel that you should make the text box associated with the input calendar disable so that a person have to use the input calendar for inserting a proper date.

    Hope this helps.
    Last edited by codeexploiter; 09-04-2008 at 03:20 AM. Reason: correction

  3. The Following User Says Thank You to codeexploiter For This Useful Post:

    meenakshi (09-04-2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default

    hi codex
    thanks once again for the help i will check and get back to u regarding this
    well yes i have indeed used a input date calender for entering the date in a pre defined format only
    it is that the code was going too big so i posted only a part of it here
    thanks once again
    smile always
    anand

  5. #4
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default

    hi codex
    i tried to use the code without any success
    it shows sometype of an error
    attaching the file along with the database for ur reference
    thanks
    anand

  6. #5
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default

    hi codex
    thanks forum
    thanks once again it is working perfectly
    smile always
    anand

  7. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I'm afraid that the code you've attached in your last posting seems to be the old one. It doesn't contain the 'formCheck' function we've been discussing. Please do check my last posting before this one. I've made a critcial change in it, which I've commented with today's date.

    Also it would be better if you can show the error that you were talking about.

  8. The Following User Says Thank You to codeexploiter For This Useful Post:

    meenakshi (09-07-2008)

  9. #7
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default

    hi codex
    thanks for the help
    and yes i had again posted the thanks after reading ur updated "formCheck(document.forms['outputForm1']);"
    and i m sorry if i have made a mistake of not uploading the main file
    i ususally edit all the work on copy file and then when the testing is finished rename it to original file name
    thanks
    smile always
    anand

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
  •