How to check field is filled

Discussion in 'PHP' started by mayuroak, Nov 4, 2007.

  1. #1
    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
     
    mayuroak, Nov 4, 2007 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    use some javascript events..

    http://w3schools.com/js/default.asp
     
    bartolay13, Nov 4, 2007 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    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;
    }
    }
     
    bartolay13, Nov 4, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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.
     
    nico_swd, Nov 5, 2007 IP
  5. rafiqasad

    rafiqasad Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    
    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:
     
    rafiqasad, Nov 5, 2007 IP
  6. undir

    undir Peon

    Messages:
    696
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can also validate fields using php
     
    undir, Nov 5, 2007 IP