user1235956
user1235956

Reputation: 13

Using Apex Validation on Text Fields?

I'm having an issue getting my validations to work correctly in Apex.

I have 3 page items that are causing me trouble, :P5_JACKPOT, :P5_TICKET_PRIZE, :P5_TOTAL_PRIZE. Jackpot can be any size, and ticket_prize + total_prize can be any size as long as they are LESS then jackpot. The validations I have in place for this are as follows:

if :P5_TICKET_PRIZE > :P5_JACKPOT then
return false;
else
return true;
end if;

Same validation for both items, with the necessary replacements, simple enough. The issue is, it doesn't seem to work for all numbers. For example, having a jackpot value of 200, and 50 for both other items cause the error to flag, when it shouldn't. However, having a jackpot value of 200, and other values of 100 + 100 don't cause the error flag, as it should. It seems that some numbers work, and others don't. Is there any reason why this is?

Upvotes: 1

Views: 3507

Answers (1)

Justin Cave
Justin Cave

Reputation: 231661

It sounds like the problem is one of data typing. :P5_TICKET_PRIZE and :P5_JACKPOT are both strings so when you compare them, you get character comparison semantics. Alphabetically, the string "50" comes after the string "200" since the character "5" comes after the character "2". If you want to compare the numeric value in :P5_TICKET_PRIZE to the numeric value in:P5_JACKPOT, you'd need to apply a to_number function to both sides of the expression

if to_number( :P5_TICKET_PRIZE ) > to_number( :P5_JACKPOT ) then
  return false;
else
  return true;
end if;

Upvotes: 3

Related Questions