470
Views
12
Comments
Solved
Custom Validation on number fields - Not empty
Question

Hi,

When checking for empty fields on forms for text I can do something like this....

variablename.name <> ""

This doesn't work for a number as it's not a string, obviously but I can't check for null either as OutSystems seems to populate empty integer fields with a 0.

How do devs work around this when working on form validation as sometime 0 is a valid entry. 

Thanks


UserImage.jpg
vikas sharma
Champion
Solution

Hi,

So for your case if 0 is a valid entry sometime then there is not any logic to check if its valid or not. As default value of integer is also 0. So will not be able to identify that this 0 is entered by user or not until don't use any event listener or key press events.

Easiest approach may be to use some value as default value (like negative values) which not suppose to be enter by user in any situation. Then on form validation you can check for this value. But in this case also you need to handling of this otherwise it will store garbage value in database.

regards



2022-09-04 15-01-10
JayPea

Hi @Vikas Sharma 

Thank you for your reply this is a good approach.

2022-09-04 15-01-10
JayPea

Hi @Vikas Sharma 

When I specify a default value myself in my structure/variable.  This is displayed in the form field.

Do you know of an easy way to handle this or is the only option to use a conditional style class to colour the text white if the value is -1 for example.

Seems like a bit of a fudge and there are also some specificity issues which arise as I'm targeting a value inside of an input field which inherits from Outsystems UI.

Any suggestions?

Thanks

UserImage.jpg
vikas sharma
Champion

Hi,

You can disable to show the default value.  In attributes put show-default-value as false. As mentioned in this post by @Nuno Reis 

https://www.outsystems.com/forums/discussion/83220/i-wanted-to-know-how-to-bind-0-zero/


2022-09-04 15-01-10
JayPea

This doesn't seem to work for me? The attribute is in the HTML when I inspect it but it has no effect?

2021-01-12 14-29-49
Prajakta Roshankhede

Hello @JayPea,

On Submit action you can convert integer to text and then compare with empty string in if condition

e.g. 

IntegerToText(GetProjectDetailById.List.Current.Price) <> "" 

Thanks

Prajakta Roshankhede

2022-09-04 15-01-10
JayPea

Thanks @Prajakta Roshankhede 

I do like this approach not sure if it will work though as it might just end up with a string of "0"?

Going to try and implement it.

Thanks

John

2022-09-04 15-01-10
JayPea

This didn't seem to work as solution as it wasn't viewed as an empty string.

2022-10-18 07-32-49
Randall Jodache Chetty

This worked for me 


2022-10-18 07-32-49
Randall Jodache Chetty

I used this to check if the integer is actually empty (as in "null").

NumberVariable <> TextToInteger("") 


2022-09-04 15-01-10
JayPea

It was actually more of a design consideration in the end.  I used radio buttons to determine if certain fields were optional and then I could validate the input with is X  > 0.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.