2 * JavaScript for signup form.
5 // When sending password by email, hide the password input fields.
7 // Always required if checked, otherwise it depends, so we use the original
8 var $emailLabel
= $( 'label[for="wpEmail"]' ),
9 originalText
= $emailLabel
.text(),
10 requiredText
= mw
.message( 'createacct-emailrequired' ).text(),
11 $createByMailCheckbox
= $( '#wpCreateaccountMail' ),
12 $beforePwds
= $( '.mw-row-password:first' ).prev(),
15 function updateForCheckbox() {
16 var checked
= $createByMailCheckbox
.prop( 'checked' );
18 $pwds
= $( '.mw-row-password' ).detach();
19 $emailLabel
.text( requiredText
);
22 $beforePwds
.after( $pwds
);
25 $emailLabel
.text( originalText
);
29 $createByMailCheckbox
.on( 'change', updateForCheckbox
);
33 // Show username normalisation warning
36 // All of these are apparently required to be sure we detect any changes.
37 events
= 'keyup keydown change mouseup cut paste focus blur',
38 $input
= $( '#wpName2' ),
39 $warningContainer
= $( '#mw-createacct-status-area' ),
44 // Hide any warnings / errors.
46 $warningContainer
.slideUp( function () {
48 .removeAttr( 'class' )
53 function updateUsernameStatus() {
55 // Leading/trailing/multiple whitespace characters are never accepted in usernames and users
56 // know that, don't warn if someone accidentally types it. We do warn about underscores.
57 username
= $.trim( $input
.val().replace( /\s+/g, ' ' ) ),
58 currentRequestInternal
;
60 // Abort any pending requests.
61 if ( currentRequest
) {
62 currentRequest
.abort();
65 if ( username
=== '' ) {
70 currentRequest
= currentRequestInternal
= api
.get( {
73 ususers
: username
// '|' in usernames is handled below
74 } ).done( function ( resp
) {
77 // Another request was fired in the meantime, the result we got here is no longer current.
78 // This shouldn't happen as we abort pending requests, but you never know.
79 if ( currentRequest
!== currentRequestInternal
) {
83 tweakedUsername
= undefined;
85 userinfo
= resp
.query
.users
[0];
87 if ( resp
.query
.users
.length
!== 1 ) {
88 // Happens if the user types '|' into the field
90 } else if ( userinfo
.invalid
!== undefined ) {
92 } else if ( userinfo
.userid
!== undefined ) {
94 } else if ( username
!== userinfo
.name
) {
100 if ( state
=== 'ok' ) {
102 } else if ( state
=== 'tweaked' ) {
104 .attr( 'class', 'warningbox' )
105 .text( mw
.message( 'createacct-normalization', username
, userinfo
.name
).text() )
108 tweakedUsername
= userinfo
.name
;
111 .attr( 'class', 'errorbox' )
114 $( '<strong>' ).text( mw
.message( 'createacct-error' ).text() ),
118 if ( state
=== 'invalid' ) {
120 .attr( 'class', 'errorbox' )
121 .append( document
.createTextNode( mw
.message( 'noname' ).text() ) )
123 } else if ( state
=== 'taken' ) {
125 .attr( 'class', 'errorbox' )
126 .append( document
.createTextNode( mw
.message( 'userexists' ).text() ) )
130 $warningContainer
.slideDown();
132 } ).fail( function () {
137 $input
.on( events
, $.debounce( 250, updateUsernameStatus
) );
139 $input
.closest( 'form' ).on( 'submit', function () {
140 // If the username has to be adjusted before it's accepted, server-side check will force the
141 // form to be resubmitted. Let's prevent that.
142 if ( tweakedUsername
!== undefined ) {
143 $input
.val( tweakedUsername
);
147 }( mediaWiki
, jQuery
) );