PDA

View Full Version : Required field(s) validation Tweak



martino
07-07-2005, 03:15 AM
Required field(s) validation
http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm

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

Martin

jscheuer1
07-07-2005, 05:10 AM
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:

if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){to this:

if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].text == "--Select--"){Then make your select box like so:

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

martino
07-07-2005, 02:03 PM
Thank you...that worked!
Much appreciated.
Martin

martino
12-14-2006, 03:23 PM
Hello,

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

I tried adding a new value:

<label><input name="rules" type="checkbox" id="rules" value="rules"> </label>

and


var fieldRequired = Array("fname", "lname", "from", "wphone", "message", "rules");


but it wouldn't work.

Any help would be greatly appreciated.

Thanks!

jscheuer1
12-14-2006, 05:45 PM
I'm not sure that you can validate a checkbox that way. To test a checkbox just do this:


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:


if(!document.forms.formname.rules.checked)

Now it returns true if unchecked, false if checked. This might be more useful in a validation script.

martino
12-14-2006, 05:56 PM
Thanks for you getting back to me on that. Where would I input that if statement though?

jscheuer1
12-14-2006, 06:17 PM
Give this a shot:



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;
}

martino
12-14-2006, 06:46 PM
That works well on its own, but I have a few other options that need to validate as well:


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;

}

}

martino
12-14-2006, 09:15 PM
no worries, I figured it out that it was the last paragraph.

Thanks for all the help...much appreciated!