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
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
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:
to this:Code:if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
Then make your select box like so:Code:if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "--Select--"){
Note that the selected option's text '--Select--' is the same as the text added in the script for testing purposes.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>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Thank you...that worked!
Much appreciated.
Martin
Hello,
I'd like to make a checkbox validation (ex. Agree to terms)
I tried adding a new value:
andCode:<label><input name="rules" type="checkbox" id="rules" value="rules"> </label>
but it wouldn't work.Code:var fieldRequired = Array("fname", "lname", "from", "wphone", "message", "rules");
Any help would be greatly appreciated.
Thanks!
I'm not sure that you can validate a checkbox that way. To test a checkbox just do this:
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.Code:if(document.forms.formname.rules.checked)
You can reverse that with:
Now it returns true if unchecked, false if checked. This might be more useful in a validation script.Code:if(!document.forms.formname.rules.checked)
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Thanks for you getting back to me on that. Where would I input that if statement though?
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
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; } }
no worries, I figured it out that it was the last paragraph.
Thanks for all the help...much appreciated!
Bookmarks