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 // Check if the username is invalid or already taken
34 mw
.hook( 'htmlform.enhance' ).add( function ( $root
) {
35 var $usernameInput
= $root
.find( '#wpName2' ),
36 $passwordInput
= $root
.find( '#wpPassword2' ),
37 $emailInput
= $root
.find( '#wpEmail' ),
38 $realNameInput
= $root
.find( '#wpRealName' ),
40 usernameChecker
, passwordChecker
;
42 function checkUsername( username
) {
43 // We could just use .then() if we didn't have to pass on .abort()…
47 apiPromise
= api
.get( {
55 uselang
: mw
.config
.get( 'wgUserLanguage' )
57 .done( function ( resp
) {
58 var userinfo
= resp
.query
.users
[ 0 ];
60 if ( resp
.query
.users
.length
!== 1 || userinfo
.invalid
) {
61 d
.resolve( { valid
: false, messages
: [ mw
.message( 'noname' ).parseDom() ] } );
62 } else if ( userinfo
.userid
!== undefined ) {
63 d
.resolve( { valid
: false, messages
: [ mw
.message( 'userexists' ).parseDom() ] } );
64 } else if ( !userinfo
.cancreate
) {
67 messages
: userinfo
.cancreateerror
? userinfo
.cancreateerror
.map( function ( m
) {
72 d
.resolve( { valid
: true, messages
: [] } );
77 return d
.promise( { abort
: apiPromise
.abort
} );
80 function checkPassword() {
81 // We could just use .then() if we didn't have to pass on .abort()…
85 if ( $usernameInput
.val().trim() === '' ) {
86 d
.resolve( { valid
: true, messages
: [] } );
90 apiPromise
= api
.post( {
91 action
: 'validatepassword',
92 user
: $usernameInput
.val(),
93 password
: $passwordInput
.val(),
94 email
: $emailInput
.val() || '',
95 realname
: $realNameInput
.val() || '',
99 uselang
: mw
.config
.get( 'wgUserLanguage' )
101 .done( function ( resp
) {
102 var pwinfo
= resp
.validatepassword
|| {};
105 valid
: pwinfo
.validity
=== 'Good',
106 messages
: pwinfo
.validitymessages
? pwinfo
.validitymessages
.map( function ( m
) {
113 return d
.promise( { abort
: apiPromise
.abort
} );
116 usernameChecker
= new mw
.htmlform
.Checker( $usernameInput
, checkUsername
);
117 usernameChecker
.attach();
119 passwordChecker
= new mw
.htmlform
.Checker( $passwordInput
, checkPassword
);
120 passwordChecker
.attach( $usernameInput
.add( $emailInput
).add( $realNameInput
) );
122 }( mediaWiki
, jQuery
) );