-
Notifications
You must be signed in to change notification settings - Fork 27.5k
input[type="number"] is valid when not a number is entered in Chrome #2144
Comments
Somebody raised this issue on the list recently. From http://www.w3.org/TR/html5/forms.html#number-state-(type=number)
and
If this problem only occurs when the browser is doing validation and the validation it's doing is the validation we want, it makes sense for the input directive to just observe the validity state of the DOM element. |
I ran into this today as well. Actually, Firefox, Chrome and IE10 each handle this scenario (entering a non-number into a number input) differently which makes it a mess.
Here is a plunker I put together based off the plunker in the Angular docs: |
This was definitely not expected. I too was wondering why "asdf" was treated as a valid numeric input in Chrome. |
Just ran into this today myself. If this isn't a bug, at least a note in the documentation would be nice. |
I ran into this as well, and noticed that the validation works when the <input type="number" ng-model="myNumber" required /> To me it feels like an Angular bug, because it doesn't add the Edit I've now tried with Firefox and there it works without the |
@joakimbeng see the comment from @jglinsek - Firefox handles the validation as expected. I think the difference is that Chrome doesn't expose the value at all when the field is filled out with something other than a number, but Firefox does. |
Also ran into this issue today. Firefox works as I expect. Chrome... not so much. Issue is that when a non-number is entered into a As @groner has pointed out, that is what the spec says to do. Although technically Chrome also shouldn't allow the input field to contain non-valid data. Pressing 'B' shouldn't produce a 'B' in the input field which would avoid this whole issue. So because non-valid input always returns empty string, the listener function in textInputType is constantly comparing empty string to empty string, and thus never calling $apply which triggers the parsing etc. This is why the number error is never being raised, since it's an empty string, which is perfectly valid. I'm happy to contribute a patch to try to solve this, but would love to get some general direction from core Angular developers as to the general strategy I should use to tackle this. |
Hey guys! Any news on this one? I'm currently using Kai's solution posted here: Is this really the solution to this issue? |
As far as I know things haven't progressed at all with this. Still an issue with Chrome. Kai's works. You could also create a simple number type directive yourself that follows the spec and use that. |
They said in that google groups discussion that validity.badInput is set to true, and it is -- The problem is that this is hard to test for. It doesn't seem to happen when the value is set programmatically and the change or input event is fired. If the value is Ultimately I sort of feel like this is a browser bug. I think checking for validity.badInput is enough to accommodate browsers which provide I've opened an issue at #304455 about it. Maybe I'm wrong, but it would be good to get people considering whether sanitizing an invalid string by simply returning an empty string is the "right thing to do". I certainly don't see any mention of doing this in the specs anywhere, unless I'm missing something. As a note: Desktop FF (at least, FF26.0a1 (2013-09-06)) does not seem to support So, I'm not sure how this is on newer versions of IE, but for other browsers it seems like we're either treated as So I think the reason why this might work well for FF is because it's actually But still, I don't see anything in the spec that says this should be the way it works, and I don't really feel like it's very helpful. |
Gave it another shot, and this sort of works. demo Definitely needs some review to improve things. |
Fixes angular#2144 On Chromium / Safari / Opera, currently: When a non-numeric string is entered into an input[type=number], the browser reports the value and innerText as the empty string. Without the ngRequire directive, the empty string is considered a valid value. This leads to false-positives. The aim of this patch is to suppress these false positives on browsers which implement the `ValidityState` object. Input directives may subscribe to check for `ValidityState` changes, and use the ValidityState during their processing. This allows the differentiation between "valid" and "invalid" empty strings.
oh god, sorry about the spam :( |
On Chromium / Safari / Opera, currently: When a non-numeric string is entered into an input[type=number], the browser reports the value and innerText as the empty string. Without the ngRequire directive, the empty string is considered a valid value. This leads to false-positives. The aim of this patch is to suppress these false positives on browsers which implement the `ValidityState` object. Input directives may subscribe to check for `ValidityState` changes, and use the ValidityState during their processing. This allows the differentiation between "valid" and "invalid" empty strings. Closes angular#2144
repro for 1.2.7 with better instructions: http://plnkr.co/edit/kuUokX?p=preview (I noticed too late that there is a PR with a fix already, so I'm posting it anyway since I updated the plunk already) |
yeah... my PR needs a lot of review, and probably needs a pretty hefty rebase since cdc4d48 was merged. I'm not really convinced it will be ready for this week, but it might not hurt to move it to 1.2.9 so I have time to clean it up a bit, and figure out how to get a more meaningful test passing. |
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
In browsers where HTML5 constraint validation is (partially) implemented, an invalid number entered into an input[type=number] (for example) input element would be visible to the script context as the empty string. When the required or ngRequired attributes are not used, this results in the invalid state of the input being ignored and considered valid. To address this, a validator which considers the state of the HTML5 ValidityState object is used when available. Closes angular#4293 Closes angular#2144 Closes angular#4857 Closes angular#5120 Closes angular#4945 Closes angular#5500
…ontainer. Related to an interesting bug in Angular. See angular/angular.js#2144 (comment)
…ontainer. Related to an interesting bug in Angular. See angular/angular.js#2144 (comment)
Is it correct to say that when an invalid number is entered in Chrome on an input of type number that it will always be marked invalid and Angular can only get a blank string? Thus there is no way to support entering commas on a numeric input. |
Pretty much :) |
When entering invalid data into second field in Chrome - second is marked as pristine and valid which is incorrect.
Possible Chrome bug - parser from numberInputType is not invoked when typing non-numerics in Chrome
live example: http://plnkr.co/edit/fULfvq?p=preview
The text was updated successfully, but these errors were encountered: