Merge "ForeignStructuredUpload: Depend on mediawiki.widgets.DateInputWidget"
[mediawiki.git] / resources / src / mediawiki.special / mediawiki.special.changeemail.js
blob06851b93720766713be432a22286b678923b8bf4
1 /*!
2  * JavaScript for Special:ChangeEmail
3  */
4 ( function ( mw, $ ) {
5         /**
6          * Given an email validity status (true, false, null) update the label CSS class
7          *
8          * @ignore
9          */
10         function updateMailValidityLabel( mail ) {
11                 var isValid = mw.util.validateEmail( mail ),
12                         $label = $( '#mw-emailaddress-validity' );
14                 // Set up the validity notice if it doesn't already exist
15                 if ( $label.length === 0 ) {
16                         $label = $( '<label for="wpNewEmail" id="mw-emailaddress-validity"></label>' )
17                                 .insertAfter( '#wpNewEmail' );
18                 }
20                 // We allow empty address
21                 if ( isValid === null ) {
22                         $label.text( '' ).removeClass( 'valid invalid' );
24                 // Valid
25                 } else if ( isValid ) {
26                         $label.text( mw.msg( 'email-address-validity-valid' ) ).addClass( 'valid' ).removeClass( 'invalid' );
28                 // Not valid
29                 } else {
30                         $label.text( mw.msg( 'email-address-validity-invalid' ) ).addClass( 'invalid' ).removeClass( 'valid' );
31                 }
32         }
34         $( function () {
35                 $( '#wpNewEmail' )
36                         // Lame tip to let user know if its email is valid. See bug 22449.
37                         // Only bind once for 'blur' so that the user can fill it in without errors;
38                         // after that, look at every keypress for immediate feedback.
39                         .one( 'blur', function () {
40                                 var $this = $( this );
41                                 updateMailValidityLabel( $this.val() );
42                                 $this.keyup( function () {
43                                         updateMailValidityLabel( $this.val() );
44                                 } );
45                         } )
46                         // Supress built-in validation notice and just call updateMailValidityLabel(),
47                         // to avoid double notice. See bug 40909.
48                         .on( 'invalid', function ( e ) {
49                                 e.preventDefault();
50                                 updateMailValidityLabel( $( this ).val() );
51                         } );
52         } );
53 }( mediaWiki, jQuery ) );