Results 1 to 9 of 9

Thread: Required field(s) validation Tweak

  1. #1
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Required field(s) validation Tweak

    Required field(s) validation
    http://www.dynamicdrive.com/dynamici...uiredcheck.htm

    This is a great script, just wondering how I can validate a drop down menu field?

    Martin

  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

    You need to modify the script a little and use a dummy selection as the default in the drop down. For example, change this line in the script:
    Code:
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    to this:
    Code:
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "--Select--"){
    Then make your select box like so:
    Code:
    <select name="Age" >
    <option selected>--Select--
    <option>0 - 15
    <option>15 - 21
    <option>21 - 30
    <option>31 - 40
    <option>41 - 50
    <option>51 - 60
    <option>Above 60
    </select></td>
    Note that the selected option's text '--Select--' is the same as the text added in the script for testing purposes.
    - John
    ________________________

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

  3. #3
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thank you...that worked!
    Much appreciated.
    Martin

  4. #4
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question how to validate a checkbox

    Hello,

    I'd like to make a checkbox validation (ex. Agree to terms)

    I tried adding a new value:
    Code:
    <label><input name="rules" type="checkbox" id="rules" value="rules"> </label>
    and

    Code:
    var fieldRequired = Array("fname", "lname", "from", "wphone", "message", "rules");
    but it wouldn't work.

    Any help would be greatly appreciated.

    Thanks!

  5. #5
    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 that you can validate a checkbox that way. To test a checkbox just do this:

    Code:
    if(document.forms.formname.rules.checked)
    where rules is the name of the checkbox and formname is the name of the form. This test will return true if checked, false if unchecked.

    You can reverse that with:

    Code:
    if(!document.forms.formname.rules.checked)
    Now it returns true if unchecked, false if checked. This might be more useful in a validation script.
    - John
    ________________________

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

  6. #6
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    Thanks for you getting back to me on that. Where would I input that if statement though?

  7. #7
    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

    Give this a shot:

    Code:
    if(!document.forms.formname.rules.checked) {
    alert('Please read the terms of acceptance!');
    return false;
    }
    else if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    - John
    ________________________

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

  8. #8
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    That works well on its own, but I have a few other options that need to validate as well:

    Code:
    function formCheck(formobj){
    
    	// Enter name of mandatory fields
    
    	var fieldRequired = Array("fname", "lname", "from", "wphone", "message");
    
    	// Enter field description to appear in the dialog box
    
    	var fieldDescription = Array("Full Name", "Last Name", "Email", "Phone", "Message");
    
    	// 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;
    
    	}
    
    }

  9. #9
    Join Date
    Jul 2005
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    no worries, I figured it out that it was the last paragraph.

    Thanks for all the help...much appreciated!

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
  •