I'm tring to validate whether the name length is met with the required value or not.
I made a code but it works in a bad way
I'm trying to see the entered name whether is less then 3 or not and return a Boolean to print the message or not.
in some cases the code prevent any entered date and the other is saving the data even if it's less then 3 .
my code is
if length(':P11_first_name') < 3 then
return true;
else
return false;
end if;
what I need to do to solve this problem.
Don't put the item name in single quotes. You're checking whether the literal string ":P11_first_name" has a length less than 3. That string will always be exactly 14 characters. You want
if length( :p11_first_name ) < 3
then
return true;
else
return false;
end if;
Related
This might be a silly one but haven't been able to figure it out.
I have a Checkbox Group Item (with Static info) with 4 different options, I want to limit the amount of checked boxes to just two. Is this something possible to make?
Thank you
A checkbox is submitted as a colon-separated string. APEX_STRING has lots of functionality to convert the string to a pl/sql collection (and back). Once converted you can use functions like FIRST, LAST, COUNT. Or even compare collections using INTERSECT. For checking a max nr, a COUNT is enough.
So the validation would be something like this (type Function Body returning Error Text):
DECLARE
l_arr apex_t_varchar2;
BEGIN
l_arr := apex_string.split(:P13_CHECKBOX,':');
IF l_arr.COUNT > 2 THEN
RETURN 'Can only select 2 values';
ELSE
RETURN NULL;
END IF;
END;
So, what I am trying to accomplish is when the number in one field is >= to a certain number then the field I want to populate will do a comparison and populate, or not depending on the condition. If the result is true the field is populated fine, but if the result is false it still performs the calculation and populates the field with " -4. I have worked trying to fix the problem for over a week. Can't seem to get it done. My variable 'total' is declared an integer.
I understand that I am comparing text and integers and that I should not compare that way and that I should convert the text to integer then calculate, but every time I try to convert something to integer I get an error code telling me that it is not a valid integer. I don't have enough experience to figure this out.
I am creating a four week budget. I buy an item, calculate how many portions there are and spread the cost through the four weeks. If there are more portions than 4 what is left over after the four week that number is calculated and automatically entered in the "leftover" field. In this code everything works perfectly, except the last if statement.It calculates and enters the data correctly, if the portions are more than 4. If they are less than 4 it writes a"-4" in the field. I cannot see my error. That is where I need help.
My Code:
procedure TForm1.costperChange(Sender: TObject);
var
Total: integer;
total1: double;
begin
Total1 := tblCosts.FieldByName('Cost').Asfloat / tblCosts.FieldByName('servings').Asfloat ;
costper.Text:= floattostr(total1);
if serv.Text >= '1' then
wk1.Text:= costper.Text
else
wk1.Text:= '';
if serv.Text >= '2' then
wk2.Text:= costper.Text
else
wk2.Text:= '';
if serv.Text >= '3' then
wk3.Text:= costper.Text
else
wk3.Text:= '';
if serv.Text >= '4' then
wk4.Text:= costper.Text
else
wk4.Text:= '';
if serv.Text >= '5' then
total:= strtoint(serv.Text);
total:= total - 4;
left.Text:= inttostr(total);
end;
I need help with a little problem. I haven't been programming long and can't figure out how to fix this. I have a small project for school and I can't figure out why I'm getting this error when validating inputs.
For example, I'm checking if the input is valid and between a min and max range by creating a function to return a true or false value based on the values entered. The reason I'm using a function is because I'm doing multiple similar checks and I figured rather than rewriting it out again, this was the best way to do this task.
Do While inputValid(string, min, max)
This is my validation, below is the simple function to validate this.
Private Function inputValid(input As String, min As Integer, max As Integer)
If Not IsNumeric(input) Then
Return False
End If
If input > min Or input < max Then
Return False
Else
Return True
End If
End Function
For some reason, despite the fact it should make sure that the value is numeric before it checks whether it's within a numerical range. It still sends me an error when I type nothing or a string in because it's trying to convert it to a double yet if I'm not doing any between range checks, it checks just fine if it's only numeric with no errors.
Can anyone help me fix this? Thanks!
You could use CInt to convert the string to Integer
Private Function inputValid(input As String, min As Integer, max As Integer)
Dim v as Integer
If Not IsNumeric(input) Then
Return False
End If
v=CInt(input)
If v < min Or v > max Then
Return False
Else
Return True
End If
End Function
You might be better off served using a regular expression to do the numeric validation on the string
Private Function inputValid(input As String, min As Integer, max As Integer)
dim regex as new Regex("[\d]+")
If not regex.isMatch(input) OrElse cint(input) > min OrElse cint(input) < max Then
Return False
Else
Return True
End If
End Function
You can use the Integer.TryParse function to check if a string can be converted to an integer.
Also, you can return a boolean which is the result of a comparison: instead of something like If x > 4 Then Return True you can use Return x > 4.
So your function could look like
Private Function StringIsValidInteger(s As String, min As Integer, max As Integer) As Boolean
Dim tmp As Integer
If Integer.TryParse(s, tmp) Then
Return (tmp >= min AndAlso tmp <= max)
End If
Return False
End Function
Sorry, guys. I was returning true and false the wrong way around it appears because I needed to continue the loop it needed to be true where it would usually be false in regular validation.
By simply changing around in the inputValid function the true and false return types, it works fine.
Thanks for your help guys.
So the problem this time around deals with charge account validation. Basically, we have to design a program in pseudocode where a user enters an account number, and the program validates it by comparing it to a list we are to put in an array. The numbers should be stored in the array, and we are required to use the sequential search algorithm to locate the number entered by the user. If the number is in the array, the program displays a message indicating it is valid. If not, it displays, invalid. There is a requirement of the main module, and the function isTicketValid. I was wondering if my pseudocode was proper. Thanks!
Constant Integer SIZE = 18
Declare String account[SIZE] = "5658845", "4520125", "7895122", 8777541", "8451277", "1302850", "8080152", "4562555", "5552012", "5050552", "7825877", "1250255", "1005231", "6545231", "3852085", "7576651", "7881200", "4581002"
Module main()
Display "Please enter your charge account number"
Input account
isValidAccount(account)
Main()
Function Integer isValidAccount(account)
Declare Boolean found
Declare Integer index
Set found = False
Set index = 0
While found == False AND index <= SIZE -1
If account[index] == account Then
Set found = True
Else
Set index = index + 1
End if
End While
If found Then
Display "Your account is valid ", index + 1
Else
Display "Your account is not valid."
End If
End Function
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?
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;