PolymerElements / paper-input

A Material Design text field
https://www.webcomponents.org/element/PolymerElements/paper-input
130 stars 162 forks source link

Can't use decimal when using allowed-pattern in paper-input #591

Closed udayghulaxe closed 6 years ago

udayghulaxe commented 6 years ago

Description

I am using allowed pattern in paper-input to restrict user to input maximum two decimal places. I am using allowed-pattern with RegEx ^[0-9]+(\.[0-9]{1,2})?$. The problem I have is that when I want to type a decimal number paper-input is not allowing me to type decimal point. I don't want to use "pattern" property and then show error message, I want to restrict user from inputting more than two decimal places.

Expected outcome

When using regex with the allowedPattern property, I want to be able to input decimal points also.

Actual outcome

I am not able to enter decimal point numbers.

Live Demo

http://plnkr.co/edit/qdcdDvIBLuuDVORb5wfF?p=preview

Steps to reproduce

  1. Add RegEx which accepts only two decimal places in allowed-pattern property.
  2. Try to add decimal numbers. Ex: 22.33
jay8t6 commented 6 years ago

+1

jay8t6 commented 6 years ago

Anyone with this issue... use this regex instead: ^(\d|-)?(\d|,).?\d$

notwaldorf commented 6 years ago

Closing, it sounds like there's a workaround.

salvita001 commented 6 years ago

this '^(\d|-)?(\d|,).?\d$' not found to me

davidmaxwaterman commented 5 years ago

NB, this is talking about decimal fractions, not simply decimal (aka base 10).

AndreSilva1993 commented 3 years ago

Today I also ran into this issue and after further investigation into the paper-input's source code, which is a styled version of the iron-input, I saw this line:

https://github.com/PolymerElements/iron-input/blob/dd04916a34ab6004e676f300f120f3b9f4da263c/iron-input.js#L328

Basically they're testing the Regex for every single character on the input's value and not the full one which is rather misleading since the property is called allowedPattern and not something along the lines of allowedCharacters. I'd suggest either one of the two following options:

Edited: I checked the documentation and it's very explicit how the allowedPattern actually works but I still think this component would benefit more from testing the entirety of the value. One can perfectly control the allowed characters by testing the whole string against the regex but it's impossible for one to control the whole input's value by testing each character individually.