Hi Guys, I want my system to check whether a single field in the form is filled by the user and if user tries to go to next menu after keeping the filled field as it is, system should give a pop up of "Do you want to save?". Without this checking user should not be able to move from tab to tab or menu to menu. Let me know if anyone knows the solution for that. Thanks in advance. Mayur
here is my sample form. include("formvalidation.js"); <form method = "post" name = "hsch_form" onSubmit="return formCheck(this);"> . .. . .... . . . ..yourForm .. .... . . . . . <input type = "submit" name = "submit" value = "Add" onClick="return checkmail(this.form.hsch_email);" /> </form> PHP: formvalidation.js function formCheck(formobj){ // Enter name of mandatory fields var fieldRequired = Array("FieldsRequired"); // Enter field description to appear in the dialog box var fieldDescription = Array("FieldsRequired"); // dialog message var alertMsg = "Fields Required:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription + "\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 + "\n"; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } }
First off, the code above would certainly not work like that. And second off, do never ever rely on javascript validation, because it can be bypassed very easily. However, I think what you're saying is that you don't want to send the form to the server for validation first. Then you'd NEED javascript (And you're in the wrong section). Just make sure you're validating on the server side as well.
function validateTextOnly($theinput,$description = ''){ $result = ereg ("^[A-Za-z0-9\ ]+$", $theinput ); if ($result){ return true; }else{ $this->errors[] = $description; return false; } } // Validate text only, no spaces allowed function validateTextOnlyNoSpaces($theinput,$description = ''){ $result = ereg ("^[A-Za-z0-9]+$", $theinput ); if ($result){ return true; }else{ $this->errors[] = $description; return false; } } // Validate email address function validateEmail($themail,$description = ''){ $result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $themail ); if ($result){ return true; }else{ $this->errors[] = $description; return false; } } // Validate numbers only function validateNumber($theinput,$description = ''){ if (is_numeric($theinput)) { return true; // The value is numeric, return true }else{ $this->errors[] = $description; // Value not numeric! Add error description to list of errors return false; // Return false } } // Validate date function validateDate($thedate,$description = ''){ if (strtotime($thedate) === -1 || $thedate == '') { $this->errors[] = $description; return false; }else{ return true; } } PHP: