How to allow only positive integer value in a textbox?

How to allow only positive integer value in a textbox?

  

I have a situation where I want users to type only positive integer values to a text box in web application.I have implemented the logic on save action to check for the value but I want the validation to happen on UI side .

Can anyone suggest on how to do so?


Thanks

Hi pranav.

You probably wants to add two extended attributes to your input, beside setting its type to Number:

min="1" and step="1"

Use the Extended Property section in the properties of the Input to do that.
Didn't tested, don't know if it will work in all browsers.

You can try also this extended property in a text input: 

pattern="\d*"


The other way is using JavaScript to capture the press of a kewy and allow only numbers. But I would avoid it.

Hope this helps.
Cheers.

Solution

Hi Pranav,

Please set the below script in text box's Extended Properties or see below snap shot.

SyntaxEditor Code Snippet

onkeypress

SyntaxEditor Code Snippet

"return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57"


Happy Coding...

cheers..

Rafi Ahmed khan

Solution

Pranav, You can also try: Input Mask

Rafi khan wrote:

Hi Pranav,

Please set the below script in text box's Extended Properties or see below snap shot.

SyntaxEditor Code Snippet

onkeypress

SyntaxEditor Code Snippet

"return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 57"


Happy Coding...

cheers..

Rafi Ahmed khan

Thanks this worked for me .


Hi Pranav,

Please mark my answer as solution..

Thanks,

Rafi