Validating a smallint field

Hi all,

I am trying to validate a smallint field that should have equal or more then 13 or equal or less then 65 and if it left null then it is ok.

if (columbia_total == null ||  columbia_total == '' &&  columbia_total >= '13' || columbia_total <= '65' )

{

}

else
{
message += 'columbia_total should be between 13 and 65.\n';
}

It informs you if you enter any number more then 65 but not less then 13.
Can some one please let me know why equal or more then 13 is not detecting any value less then 13.

Thanks for any help.

Abrahim

akalehzan:
‘13’ || columbia_total <= ‘65’

Hi, looks like you are comparing text rather than integers…

Agree with Christian but would also add that the value comparison is:

= 13 && <= 65

Also some of the arguments should be logically grouped using brackets as follows:

if ( ( columbia_total == null || columbia_total == '' ) && ( columbia_total >= 13 && columbia_total <= 65 ) )

Cheers
Harry

Christian and Harry,

Following the suggestion from both of you did help:-)

Thank you for all the help.

Abrahim

Hi Abrahim,

I am embarassed to say that my response was incorrect also when I read my post :oops:

It should be this:

if ( columbia_total == null || columbia_total == '' || ( columbia_total >= 13 && columbia_total <= 65 ) )

So you test for :
If it is null
OR
if it is empty
OR
( if it is greater than or equal to 13 and less than or equal to 65 )

Apologies
Harry

Except for a number can never be ‘’. So shorter is

if ( !columbia_total || ( columbia_total >= 13 && columbia_total <= 65 ) )

Sorry for the wrong info.

What I meant was:

If it is not null
OR
if it is not empty

and

( if it is greater than or equal to 13 and less than or equal to 65 )

Here is what working so far:

if ( ( columbia_total != null ||  columbia_total != '' ) && ( columbia_total >= 13 && columbia_total <= 65 ) )

Patrick,

in your code:

if ( !columbia_total || ( columbia_total >= 13 && columbia_total <= 65 ) )

What does !columbia_total means?

Thanks,

Abrahim

Hi Abrahim,

Patrick’s code is wonderfully precise and the ‘!columbia_total’ returns a boolean result which literally means that columbia_total column has nothing in it.

The ‘!’ is javascript operator for ‘NOT’

That said, you have now clarified that it must have a value and that value must be >=13 and <=65 so you should be simplifying the test to:

if ( columbia_total && ( columbia_total >= 13 && columbia_total <= 65 ) ) 

The first ‘columbia_total’ tests if there is a value in the column and then it goes on to compare the value in there against 13 and 65

Cheers
Harry

Why not just

if ( columbia_total >= 13 && columbia_total <= 65 ) ) 

?

saskia:
Why not just

if ( columbia_total >= 13 && columbia_total <= 65 ) ) 

?

This looks good to me, as integers can’t be null.

! columbia_total

just checks that columbia_total is not 0, which is outside the range anyway :wink:

Hi saskia,

saskia:
Why not just

if ( columbia_total >= 13 && columbia_total <= 65 ) ) 

?

Agreed - getting carried away with validation :oops:

Cheers
Harry

You have been very kind with all the useful feedback.

They are all useful to me.

Thanks :D

Abrahim