Whole numbers only please

I cant find anywhere the ability to stop users entering numbers after a decimal point.

I have a field where I have defined whole numbers only but if a user miss keys and enters a decimal number there is no way of stopping them.

As a stopgap I have created a second field showing two decimal points and making it the same as the original so at least we can see if someone has done this but we are entering some 1,000+ records a month so not ideal.

Is there a way in Knack I have missed or can this be done in Java (which I am a complete novice).

Any help gratefully received.

Wha kind of numbers are they adding?
Are they a defined range, like 1-100 or can they be any whole number?

Hi Carl
Any numbers from 1-1,000. A miss key can produce say 265.1 and it will only show 265 as no decimal places are show, I need a way of validating to only whole numbers.
John

I’ve been thinking about this on and off all day :thinking: I couldn’t find a way to make, as an example, 245.1 just show 245

I know that we can use zero decimal places so the value in the table shows “245” but when you click into it, it’s really “245.1”

I guess your just trying to remove the .1

Validation and Conditional rules don’t help for this situation. I even tried to see if I could do something with Integromat, before going to the cinema to see the new bond film :movie_camera: :facepunch:

Round or ceil are a couple of useful functions but they just round the value up :arrow_up: or down :arrow_down: to the nearest whole number.

The reason I asked if there was a numeric range is that I have similar situation with a ceramic tiling company and the staff were often putting decimal places into the values.

The quantity was never going to be higher than 50/60 in my case, so I added a related table with numbers 1-100 and connected it to the parent table as a searchable field.

It seems a bit of a waste of 1,000 records to do the same, however, with the connection set as searchable users can type “23X” and get the range from 230-239 quickly to select the correct whole value.

From memory, I then had a number field with a conditional rule looking at the field so I capture the number as it was being used for a calculation.

Not sure if you could get around this with code but as I’m not a coder the above is all I’ve got :facepunch:

The closest I’ve found is this from html - How to avoid Decimal values in input type number - Stack Overflow

<input type="number" onkeydown="return event.keyCode !== 190">

If I have time (or someone jumps in first) we’ll work on some Javscript to inject the onkeydown attribute to your field.

Do you know the field number you want to limit to integers?

Aircode not tested:

$(document).on('knack-view-render.any', function(event, view, data) {
      $('#field_xxx').prop('onkeydown','return event.keyCode !== 190')
})

Thanks for jumping in @BradStevens - thought there might be a code based solution out there :+1: I’ll leave it to the coders to resolve :stuck_out_tongue_closed_eyes:

This works - found a use case for us and replace xxx with your field number.
Be aware that all it does is ignore the decimal entry.

$(document).on('knack-view-render.any', function(event, view, data) {
      $('#field_xxx').attr('onkeydown','return event.keyCode !== 190')
})

First thanks to you both for your prompt reply and input which is very much appreciated.
Next… I cant make it work :frowning: I have pasted the code as above with the field number substituted for the XXX but it makes no difference, still accepts and shows numbers after the decimal point.

Should I be doing something with the “190”?

Somehow the event isn’t being triggered so we’ll need to problem solve. 190 is the keyCode for ‘.’ so that doesn’t change.

Can you share a screenshot of your browser inspector for the field?
For chrome, right click on the input box and choose Inspect.

That will result in displaying your HTML so we can look at the element we’re targeting.

There is one potential issue with just preventing the entry of a decimal point (the code above) and that is that it does NOT prevent the next numbers being added and so you could have a very wrong entry.

I have tried a few things and the best I have come up with is to have an Equation to work out if the number is a whole number:

{Enter Integer Number} == round({Enter Integer Number},0) ? 1:0

The check the value of this in a Submit Rule and if it is Zero go to a new child page to put it right - if not submit as normal:

Not that simple but avoids code which is always a good thing?

2 Likes

Is this what you need Brad?

AHA! Using inline table cell editing and not a standard form, that’s why it wasn’t working. Let’s see if we can have a go at covering that scenario.

And kudos to @JulianKirkness no code method worth considering above - may or may not be simple for inline table editing but I have a suspicion this could be the way to go given the inline editing needs.

OK this is where I am at. I have used another field as suggested above by Julian and it works a treat. I had tried something like this before with a Text Formula field and using the right function and looking something but 00 but that meant me displaying to users two decimal places which could cause confusion, Julians way is much better.

I was looking at a way of validation on input because I was trying to stay away from more calculation fields on this app as it is slowing down somewhat because of its complexity but I guess one more won’t hurt :slight_smile: .

Thanks to you both for your time and effort on this one. :+1:

@JulianKirkness no code wins mate!

1 Like

:grinning_face_with_smiling_eyes:!

Julian, I am using this with great success, one thing I found out is that the “round” function is not supported nor advertised by knack so without you I wouldn’t have found it, so thanks again.