Using JavaScript if - else statements to return null

Hi all,

I have the following method that suppose to do field validation and if any field left blank then return null into globals.Text_Box2, and if not null then return the data into the globals.Text_Box2:

var space = " ";
var cma = ",";
var dot = ".";
var rtrn = "\r";
var nline = "\n";
/////////starting second table fields
////////////// tb2 field 1
if ( globals.SQL_F2 == null)
            {globals.SQL_F2 == null  }
            else {var sq2f2 = globals.SQL_Table2 + dot + globals.SQL_F2}
 
 
////////////// tb2 field 1-1
if ( globals.tb2f1 == null)
            {globals.tb2f1 == null   }
            else {var tb2f1 = cma + globals.SQL_Table2 + dot + globals.tb2f1}
 
//////////////table2 field 2
if ( globals.tb2f2 != null)
            {var tb2f2 = cma + globals.SQL_Table2 + dot + globals.tb2f2}
            else {tb1f2 = null}
            
/////////////tb2fiels3          
            
if ( globals.tb2f3 != null)
            {var tb2f3 = cma + globals.SQL_Table2 + dot + globals.tb2f3}
            else {tb2f3 = null}
            
/////////////tb2fiels4
if ( globals.tb2f4 != null)
            {var tb2f4 = cma + globals.SQL_Table2 + dot + globals.tb2f4}
            else {tb2f4 = null}
            
            
/////////////////////////////////////////////////tb1f5
if ( globals.tb2f5 != null)
            {var tb2f5 = cma + globals.SQL_Table2 + dot + globals.tb2f5}
            else {tb2f5 = null}
 
//////////////////////////////////////////////tb1f6
if ( globals.tb2f6 != null)
            {var tb2f6 = cma + globals.SQL_Table2 + dot + globals.tb2f6}
            else {tb2f6 = null}
 
globals.Text_Box2 =  sq2f2 + nline  + tb2f1 + nline + tb2f2 + nline + tb2f3 + rtrn + tb2f4 + nline + tb2f5 + nline + tb2f6

Let’s say if I typed ABC in the first field “sq2f2”, then in the “globals.Text_Box2” it returns:

Table2.ABC
undefined
undefined

Con some one please let me know why and how can I avoid having “undefined undefined” characters returned in the “globals.Text_Box2”.

Or is there a better way to do this?

Thanks in advance,

Abrahim

Hi,

you need to make sure you declare your variables so they both cases of the if statements.

var myvar = '';
if(something) myvar = somevalue;

of

if(something) {
   var myvar = somevalue;
} else {
   var myvar = '';
}

Hope this helps,

Thanks Christian!

Abrahim