95
Views
7
Comments
How can I restrict the value entered in the input box to only whole numbers?
Question

hi,

How can I restrict the value entered in the input box to only integers? 

not to enter a negative value or a decimal value in the input box 

2024-05-14 06-52-28
Srigovindh-EONE

hi Rakesh

https://personal-wcnuuiyu.outsystemscloud.com/OutsystemBasic/maxlength?_ts=638518118051180802


this script is added on 'onkeypress' event on widget 


if(event.charCode >= 48 && event.charCode <= 57 )

{

var val = document.getElementById($parameters.Inputvar).value;

var len = val.length;

if(len < $parameters.maxlength)

{

var newval = val + String.fromCharCode(event.charCode);

if(newval <= 100)

return true;

}

else

event.preventDefault();

}

else

event.preventDefault()


Please refer to attached OML file.

maxlength(1).oml
2024-11-05 11-28-22
Sunil Rajput

Hii, Rakesh M 

I have used java script in onkeydown handler in input field.

if (event.key === 'e' || event.key === '+' || event.key === '-') {    event.preventDefault();}

Folder name - word and screen name - integer

https://sunil-rajput.outsystemscloud.com/DailyTask/Integer?_ts=638518082196264188

Please refer to attached OML file 

DailyTask.oml
2024-05-22 06-12-56
Vignesh Prakash

Hi Sunil, 

The above implementation will allow decimal values where as the requirement was to allow only positive integers without a decimal.

UserImage.jpg
Janani Thangadurai

Hi @Rakesh M ,

If you are not comfortable with JavaScript or regex, you can opt for the InputMaskReact forge component. With this, you can achieve formatting for whole numbers and specific formats like (99) 333-4555, and also restrict the maximum length.

Please refer to attached OML file.



InputValidation.oml
2024-05-22 06-12-56
Vignesh Prakash

Hi @Rakesh M

Please have a look at the attached OML and let me know if it helps.

Thanks,
VP.

Input_updated.oml
2021-11-12 04-59-31
Manikandan Sambasivam

@Rakesh M  

Add the restrictToWholeNumbers function to the onKeyPress event of the input field in the Properties panel.

Use the following JavaScript code in the client action to restrict input to whole numbers only

function restrictToWholeNumbers(event) {

    const regex = /^[0-9\b]+$/; // Regex to match whole numbers and backspace

    const key = event.key; // Get the pressed key

    if (!regex.test(key)) {

        event.preventDefault(); // Prevent input if not a digit or backspace

    }

}

2024-05-06 07-41-12
Narendra Tiwari

Hi Rakesh M ,

You can use regex pattern for whole number input box it will restrict other entries.

Thanks,

Narendra Tiwari 

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