views:

64

answers:

2

Hi,

I am validating some fields and check if the length of a select element is larger than 0. I get the error "'length' is null or not an object" because id$=SelectResult is a listbox and can have no values and therefor return null and var val = $(this).val(); doesn't like that.

function checkControls() {
  var itemLevel = $("select[title='Item Level']").val();
  switch (itemLevel) {
    case 'Strategic Objective':

 var controlsPassed = 0;


    $("input[id$=UserField_hiddenSpanData],input[title=Title],select[id$=SelectResult]").each(function(){


        var val = $(this).val();
        if(val != 0 && val.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
return (controlsPassed == 3) 

    case 'Milestone Action':

      var controlsPassed = 0;


    $("input[title=Target Date],select[id$=SelectResult],input[title=Title],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic 

Objective],select[title=Strategic Priority]").each(function(){


        var val = $(this).val();
        if(val != 0 && val.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
return (controlsPassed == 7) 

case 'Performance Measure':

      var controlsPassed = 0;


    $("select[title=Strategic Objective],input[title=Title],select[id$=SelectResult],select[title=Strategic Priority]").each(function(){

        var val = $(this).val();
        if(val != 0 && val.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
return (controlsPassed == 4) 


    case 'Strategic Priority':

      var controlsPassed = 0;


    $("input[title=Target Date],select[id$=SelectResult],input[title=Title],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic 

Objective]").each(function(){   

        //var ResponsibleBusiness = $("select[id$=SelectResult]").val();
        var val = $(this).val();
        if(val != 0 && val.length != 0) { 

            //add one to the counter
            controlsPassed += 1;
        }

        });
return (controlsPassed == 6) 
  }
}

function PreSaveItem() {
            return checkControls()
    }
A: 

If I'm understanding your goal correctly, you can shorten it down to this:

if(!$("select[id$=SelectResult]").val()) return false;
return $("input[title=Target Date],input[title=Title],input[id$=UserField_hiddenSpanData],input[title=Start Date],select[title=Strategic Objective]").filter(function(){
         return $(this).val() == '';
       }).length > 0;
Nick Craver
Hi, I just updated my question a bit.
Peter
@Peter - What error are you getting exactly?
Nick Craver
"'length' is null or not an object" if the user clicks the submit button when there's nothing in the list (id$=SelectResult). If the user adds an item to the list they can hit submit. I don't want them to be able to submit if there's no item in the list so it works, I just want to get rid of the error.
Peter
So I thought I could use something like var testVariableLength = (testVariable ? testVariable.length : 0);but can't get the syntax right when I'm using jQuery.
Peter
@Peter - What does replacing that line with `var srVal = $("select[id$=SelectResult]").val(), ResponsibleBusiness = srVal ` get you?
Nick Craver
I still get the same error because I am using $(this).val() and if that list doesn't have any item it returns null
Peter
@Peter - There's no need to have `select[id$=SelectResult]` in that selector if you're already checking it...but did you try my complete replacement above? It's much easier :)
Nick Craver
Yeah I just realized that, I don't know how to use your code though. I updated my question with the whole validation block.
Peter
@Peter the code above would replace your entire function and return true/false.
Nick Craver
A: 

Change this line:

if(val != 0 && val.length != 0) { 

to

if ( !!val && val.length > 0) {
HS