angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

IE10 fires input event when a placeholder is defined so that form element is in dirty instead of pristine state

Open jupiterplanet opened this issue 13 years ago • 27 comments

Input elements in AngularJS can be in pristine state meaning that the input element's value hasn't been modified yet by the user. In this case error messages can be hidden although validation fails because the input field is empty.

AngularJS uses different measures to determine if the value in an input element has changed. It listens to the input event for all browsers supporting this event with the exception of IE9 as this browser does not implement this event correctly.

However, as it turns out IE10 also has implementations faults. If a placeholder is defined on an input element IE10 fires this event when the placeholder is set during DOM loading and when it is removed when the user clicks into the input field so that error messages which depend on the pristine condition are displayed although the user has not yet modified the input's value.

Open the following plunk with IE10 to see this behavior: http://plnkr.co/LqlkhtIPPEwgP0znR7vz

Click into the e-mail input field and the error message displays although the input's value has not changed yet.

In all other browsers (Chrome, IE9, Firefox) the error message is not displayed.

A workaround is to not use the input event just like for IE9.

config(['$provide', function($provide) {
        $provide.decorator('$sniffer', ['$delegate', function($sniffer) {
            var msie = parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
            var _hasEvent = $sniffer.hasEvent;
            $sniffer.hasEvent = function(event) {
                if (event === 'input' && msie === 10) {
                 return false;
                }    
                _hasEvent.call(this, event);
            }
            return $sniffer;
        }]);
    }]).

jupiterplanet avatar May 09 '13 11:05 jupiterplanet

@caitp will this be fixed in 1.2.x branch as well?

chiefjester avatar Apr 22 '14 15:04 chiefjester

@deezahyn this was merged into 1.2 and 1.3. There's also a bug report on IE about this behaviour, so perhaps there will eventually be a fix for it. Unfortunately, placeholder change isn't the only time IE sends input events inappropriately.

It will also send an input event if you so much as click on a form control with a placeholder :( So this fix doesn't totally make IE behave normally =(

caitp avatar Apr 22 '14 15:04 caitp

oops my bad, thought it was only for 1.3.x tag. Will now test :+1: Thanks for the fix @caitp

chiefjester avatar Apr 22 '14 15:04 chiefjester

~~I'm still getting some associated problems, It seems like even though it doesn't cause the model to be dirty, it still can update the scope with the viewValue...~~

~~This can cause issues if for example ng-model="container.value" container will be created on the current scope, whereas the 'real' container is actually supposed to be on $parent but hasn't been created yet.~~ Oops, merged in 1.2.17 but using 1.2.16

jamie-pate avatar Jun 11 '14 20:06 jamie-pate

Sorry to resurface this issue, but this bug still seems to be present for textareas. I'm using AngularJS 1.2.22 and testing in IE 11 on Windows 7.

tgt avatar Aug 28 '14 09:08 tgt

This issue seams to still be present (input type='text' with placeholder) in IE11 and AngularJS 1.3.2. I haven't tested on IE10 yet. Following hack (coffee script) based on initial @jupiterplanet solution works for me:

.config ($provide) ->
  $provide.decorator '$sniffer', ['$delegate', ($sniffer) ->
    userAgent = navigator.userAgent.toLowerCase()
    msie = parseInt((/msie (\d+)/.exec(userAgent) ? [])[1], 10)
    if isNaN(msie)
      msie = parseInt((/trident\/.*; rv:(\d+)/.exec(userAgent) ? [])[1], 10)
    _hasEvent = $sniffer.hasEvent
    $sniffer.hasEvent = (event) ->
      if event is 'input' and msie >= 10
        return false
      _hasEvent.call this, event
    return $sniffer
  ] 

ognus avatar Nov 18 '14 23:11 ognus

For others who are coming across this issue when using textareas , it appears to have been fixed as of 1.3.6.

Plunker (open in IE): http://plnkr.co/edit/LDZX0GFvq1zchiVGehCU?p=preview

When opened with IE 11.0.9600.17416, change the Angular version in the plunker between 1.3.5 and 1.3.6 and you'll see the form no longer getting set to dirty as of 1.3.6.

lijuenc avatar Mar 03 '15 04:03 lijuenc