# Whole numbers only please

**URL:** <https://forums.knack.com/t/whole-numbers-only-please/11262>\
**Category:** Ask Developers\
**Created:** [October 3, 2021, 10:23am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262 "2021-10-03T10:23:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 3, 2021, 10:23am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/1 "2021-10-03T10:23:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)\
**Post date:** [October 3, 2021, 6:02pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/2 "2021-10-03T18:02:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 3, 2021, 6:37pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/3 "2021-10-03T18:37:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)\
**Post date:** [October 3, 2021, 9:27pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/4 "2021-10-03T21:27:30Z")

</div>

I’ve been thinking about this on and off all day 🤔 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 🎥 👊

Round or ceil are a couple of useful functions but they just round the value up ⬆ or 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 👊

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [October 3, 2021, 10:01pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/5 "2021-10-03T22:01:28Z")

</div>

The closest I’ve found is this from [html - How to avoid Decimal values in input type number - Stack Overflow](https://stackoverflow.com/questions/37043867/how-to-avoid-decimal-values-in-input-type-number)

```auto
<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:

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

```

---

<div class="post-metadata">

**Author:** ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)\
**Post date:** [October 4, 2021, 5:13am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/6 "2021-10-04T05:13:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [October 4, 2021, 7:45am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/7 "2021-10-04T07:45:44Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 4, 2021, 9:52am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/8 "2021-10-04T09:52:43Z")

</div>

First thanks to you both for your prompt reply and input which is very much appreciated.  
Next… I cant make it work ☹ 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”?

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [October 6, 2021, 9:04am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/9 "2021-10-06T09:04:12Z")

</div>

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.

 ![2021-10-06](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/7/72bd5fb1fb3a8064fd4239ab8dfb0a9dc5d67bf1.png)

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

 ![2021-10-06 (1)](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/d/dd14e7601d6c5ec0f33dae33a8925b70129d3478.png)

---

<div class="post-metadata">

**Author:** ![JulianKirkness](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/juliankirkness/32/770_2.png) [@JulianKirkness](https://forums.knack.com/u/JulianKirkness)\
**Post date:** [October 6, 2021, 12:23pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/10 "2021-10-06T12:23:12Z")

</div>

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:

 ![Screenshot 2021-10-06 at 13.21.57](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/1/1c2a441494435e9ec566de0332c047511b0287fc.jpeg)

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

---

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 6, 2021, 12:49pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/11 "2021-10-06T12:49:20Z")

</div>

![Screenshot 2021-10-06 at 13.27.39](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/6/6463fdb55bc9706220cf982ba17dc2ce529727d1.png)

Is this what you need Brad?

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [October 6, 2021, 8:12pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/12 "2021-10-06T20:12:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 7, 2021, 1:18pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/13 "2021-10-07T13:18:09Z")

</div>

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 🙂 .

Thanks to you both for your time and effort on this one. 👍

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [October 7, 2021, 8:04pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/14 "2021-10-07T20:04:12Z")

</div>

@JulianKirkness no code wins mate!

---

<div class="post-metadata">

**Author:** ![JulianKirkness](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/juliankirkness/32/770_2.png) [@JulianKirkness](https://forums.knack.com/u/JulianKirkness)\
**Post date:** [October 7, 2021, 9:33pm UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/15 "2021-10-07T21:33:01Z")

</div>

😄!

---

<div class="post-metadata">

**Author:** ![JohnG](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JohnG](https://forums.knack.com/u/JohnG)\
**Post date:** [October 13, 2021, 11:56am UTC](https://forums.knack.com/t/whole-numbers-only-please/11262/16 "2021-10-13T11:56:43Z")

</div>

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.
