PDA

View Full Version : Required field validation Select Forms



ryans
08-21-2011, 05:25 AM
1) Script Title: Required Field Validation

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm

3) Describe problem:
someone had posted on how to check if forms are being filled out or not. It looks for the word Select and if it's still in that state it will prompt the user.

It works fine when using chrome, but IE8 or IE9 it just skips that part. See screenshots.


Here is the 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", "Email", "Address", "City", "State", "ZipCode", "PhoneNumber", "Card1", "Card2", "Card3", "Card4", "expiration", "Security", "Name_On_Card", "vname", "vaddress", "vcity", "vstate", "vzip", "vphone1", "vcontact", "vphone", "signed");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name", "Email", "Address", "City", "State", "Zip Code", "Phone Number", "Credit Card 1", "Credit Card 2", "Credit Card 3", "Credit Card 4", "Expiration Date", "Credit Card Security Code", "Name On Credit Card", "Venue Name", "Venue Address", "Venue City", "Venue State", "Venue Zip Code", "Phone Number", "Primary Contact", "Primary Contact Phone Number", "Signature");
// 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-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
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.BeginTime;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Start Time' +'\n';
}


var opt=document.formcheck.EndTime;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'End Time' +'\n';
}


var opt=document.formcheck.eventtype;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Event Type' +'\n';
}

var opt=document.formcheck.Booths;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Number of Booths' +'\n';
}

var opt=document.formcheck.menu-844;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Print Choice' +'\n';
}

var opt=document.formcheck.artwork;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Artwork' +'\n';
}


var opt=document.formcheck.Early;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Load In' +'\n';
}


var opt=document.formcheck.Mileage;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Delivery Mileage' +'\n';
}

var opt=document.formcheck.Insurance;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'General Liability Insurance' +'\n';
}

var opt=document.formcheck.workers;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Workers Comp Insurance' +'\n';
}

var opt=document.formcheck.auto;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Auto Insurance' +'\n';
}

var opt=document.formcheck.vendormeal;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Vendor Meal' +'\n';
}

var opt=document.formcheck.smonth;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Start Month' +'\n';
}

var opt=document.formcheck.sday;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Start Day' +'\n';
}

var opt=document.formcheck.syear;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Start Year' +'\n';
}


var opt=document.formcheck.emonth;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'End Month' +'\n';
}

var opt=document.formcheck.eday;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'End Day' +'\n';
}

var opt=document.formcheck.eyear;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'End Year' +'\n';
}

var opt=document.formcheck.bstate;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Billing State' +'\n';
}

var opt=document.formcheck.State;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'State' +'\n';
}

var opt=document.formcheck.vstate;
if(opt.value=='Select')
{
alertMsg +=' - ' + 'Venue State' +'\n';
}



if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>


http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4032&d=1313904285

http://www.dynamicdrive.com/forums/attachment.php?attachmentid=4031&d=1313904268

ryans
09-02-2011, 09:49 PM
bump :)