Advanced Search

Results 1 to 5 of 5

Thread: form validation using a select field

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default form validation using a select field

    1) Script Title:
    Required field(s) validation
    2) Script URL (on DD):
    http://www.dynamicdrive.com/dynamici...uiredcheck.htm
    3) Describe problem:

    Is this script supposed to work on a 'select' field?

    Works great for everything else except select.
    e.g.
    HTML Code:
    <select name="selectsomething"> 
    	<option value="" selected="selected">Please Select...</option>
    	<option value="opt1">OPT 1</option>
    	<option value="opt2">OPT 2</option>
    </select>
    If no option is selected the form still validates.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You need to set it in your JS:
    Code:
    <script language="JavaScript">
    <!--
    
    /***********************************************
    * Required field(s) validation v1.10- By NavSurf
    * Visit Nav Surf at http://navsurf.com
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    function formCheck(formobj){
    	// Enter name of mandatory fields
    	var fieldRequired = Array("FirstName", "LastName","selectsomething");
    	// Enter field description to appear in the dialog box
    	var fieldDescription = Array("First Name", "Last 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";
    				}
    			}
    		}
    	}
    	var opt=document.formcheck.selectsomething;
    	if(opt.value=='')
    	{
    	alertMsg +=' - ' + 'selectsomething' +'\n';
    	}	if (alertMsg.length == l_Msg){
    		return true;
    	}else{
    		alert(alertMsg);
    		return false;
    	}
    }
    // -->
    </script>
    See highlighted
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    It doesn't seem to work.

    With that extra code there none of the fields appear to be checked.

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    What you mean by not working?..Have you changed the higlighted to what your form name is:
    Code:
    var opt=document.formcheck.selectsomething;
    ...also, selectsomething is your select element's name, which I'm basing from your previous post.
    If nothing works, could you up your markups (HTML) as well
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    ah yes, my mistake.

    Thanks for your help

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
  •