Results 1 to 4 of 4

Thread: Dynamic Alert Box

  1. #1
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dynamic Alert Box

    I have coded a simple web form validation script which goes through all the mandatory fields when a users hits the submit button, and produces an alert box telling the user what field is not filled in. However it produces an alert box for each field, individually, which is slightly annoying. Do you know of a script which will produce a dynamic alert box, one tells you to fill in all mandatory fields that are empty, not just one at a time? Thank you in advance.

    My code is below

    Code:
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {
    with (field)
    {
    if (value==null||value=="")
     {
    	alert(alerttxt);
    	return false;
     }
    else {return true}
    }
    }function validate_form(thisform)
    {
    with (thisform)
    {
    	var name = "Patient ID box must be filled out to continue!"
    	if (validate_required(patientid, name)==false)
    	  {patientid.focus();return false}
    
    	else if (validate_required(dob, "Date of Birth box must be filled out to continue!")==false)
    	  {dob.focus();return false}
    	  
    	else if (validate_required(age, "Age box must be filled out to continue!")==false)
    	  {age.focus();return false}
    
    	else if (validate_required(height, "Height box must be filled out to continue!")==false)
    	  {height.focus();return false}
    	 
    	else if (validate_required(weight, "Weight box must be filled out to continue!")==false)
    	  {weight.focus();return false}
    
    	else if (validate_required(sex, "Check a Sex button to contunue!")==false)
    	  {!sex.checked();return false}
    
    	else if (validate_required(permission, "Choose Permission Yes or No to contunue!")==false)
    	  {!permission.checked();return false}
    
    }
    
    	return true;
    
    }
    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure how to handle your sex and permission check boxes/radio buttons, whatever they are. I coded for check boxes that set the value of hidden fields. But I cannot see your markup, so I cannot be sure of exactly how to handle them. Other than that, something like so:

    Code:
    <script type="text/javascript">
    function validate_required(field){
    if (field.value==null||field.value=="")
    	return false;
    return true;
    }
    function validate_form(thisform){
    var alerttxt='', f=thisform, fc=null;
    
    	if (!validate_required(f.patientid)){
    	alerttxt += "Patient ID box must be filled out to continue!\n";
    	  fc=f.patientid;
    	  }
    
    	if (!validate_required(f.dob)){
    	alerttxt += "Date of Birth box must be filled out to continue!\n";
    	  fc=fc||f.dob;
    	  }
    	  
    	if (!validate_required(f.age)){
    	alerttxt += "Age box must be filled out to continue!\n";
    	  fc=fc||f.age;
    	  }
    
    	if (!validate_required(f.height)){
    	alerttxt += "Height box must be filled out to continue!\n";
    	  fc=fc||f.height;
    	  }
    	 
    	if (!validate_required(f.weight)){
    	alerttxt += "Weight box must be filled out to continue!\n";
    	  fc=fc||f.weight;
    	  }
    
    	if (!validate_required(f.sex)){
    	alerttxt += "Check a Sex button to contunue!\n";
    	  fc=fc||f.male; //male should be the name of one of the checkboxes
    	  }
    
    	if (!validate_required(f.permission)){
    	alerttxt += "Choose Permission Yes or No to contunue!\n";
    	  fc=fc||f.yes; //yes should be the name of one of the checkboxes
    	  }
    
    	if (alerttxt){
    		alert(alerttxt);
    		fc.focus();
    		return false;
    	}
    	return true;
    
    }
    </script>
    Last edited by jscheuer1; 08-10-2007 at 02:16 AM. Reason: neatness counts
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I use this:

    Code:
    function checkForm(formobj){
    
    		var fieldRequired = Array("name", "msg"); // actual names of fields
    
    		var fieldDescription = Array("Name", "Message"); // what you want to call it in the alert
    
    		var alertMsg = "The following form fields were left empty:\n\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;
    		}
    	}
    And you would use it like so:

    HTML Code:
    <form method="post" action="whatever.php" onsubmit="return checkForm(this);">
    ...
    I'm not entirely sure if it works on textboxes, either, though. Guess you'll have to see. Haha.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for both of your responses but I actually fooled around with the code a bit and found a simpler option. (see code below) I am so happy that I have realized the enjoyment I recieve while coding, im currently writing some stuff in asp.net based on thos basic html pages before. The community is very helpful and the people so nice, got it is good to be a geek. : )

    -Andrew

    Code:
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {
    	with (field)
    	{
    	if (value==null||value=="")
    
    	 {
    		//alert(alerttxt);
    		return false;
    	 }
    	else {return true}
    
    	}
    }
    
    function radio_button_checker()
    {
    	// set var radio_choice to false
    	var radio_choice = false;
    
    	// Loop from zero to the one minus the number of radio button selections
    	for (counter = 0; counter <all_form.sex.length; counter++)
    	{
    	// If a radio button has been selected it will return true
    	// (If not it will return false)
    	if (all_form.sex[counter].checked)
    	radio_choice = true; 
    	}
    
    	if (!radio_choice)
    	{
    	// If there were no selections made display an alert box 
    	//alert("Please select a sex.")
    	return (false);
    	}
    	return (true);
    }
    
    
    function validate_form(thisform)
    {
    	with (thisform)
    	{
    		//var name = "Patient ID box must be filled out to continue!"
    
    				var errMsg = "";
    
    		if (validate_required(patientid, name)==false)
    		  {patientid.focus(); errMsg = "Patient ID is required"; //return false
    		  }
    
    		if (validate_required(dob, "Date of Birth box must be filled out to continue!")==false)
    		  {dob.focus(); errMsg = errMsg + "\nDOB is required"; //return false
    		  }
    		  
    		if (validate_required(age, "Age box must be filled out to continue!")==false)
    		  {age.focus(); errMsg = errMsg + "\nAge required"; //return false
    		  }
    
    		if (validate_required(height, "\nHeight box must be filled out to continue!")==false)
    		  {height.focus(); errMsg = errMsg + "\nHeight required"; //return false
    		 }
    		 
    		if (validate_required(weight, "Weight box must be filled out to continue!")==false)
    		  {weight.focus(); errMsg = errMsg + "\nWeight required"; //return false
    		 }
    
    		if (radio_button_checker(sex, "")==false)
    		  {sex.checked;  errMsg = errMsg + "\nPlease select a sex"; //return false
    		  }
    
    	}
    
    			//var name = "arathi"
    			//VAR NAME2= "RAVI;
    			//NAME = NAME + "RAVI"
    	
    	if (errMsg != "" )
    	{
    		alert(errMsg);
    		return false;
    	}
    
    	 return true;
    
    }
    </script>
    Last edited by mastershake; 08-14-2007 at 02:59 PM. Reason: adding some nice comments : )

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
  •