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; show username normalisation warning
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()…
46 // Leading/trailing/multiple whitespace characters are always stripped in usernames,
47 // this should not require a warning. We do warn about underscores.
48 username
= username
.replace( / +/g
, ' ' ).trim();
51 apiPromise
= api
.get( {
59 uselang
: mw
.config
.get( 'wgUserLanguage' )
61 .done( function ( resp
) {
62 var userinfo
= resp
.query
.users
[ 0 ];
64 if ( resp
.query
.users
.length
!== 1 || userinfo
.invalid
) {
65 d
.resolve( { valid
: false, messages
: [ mw
.message( 'noname' ).parseDom() ] } );
66 } else if ( userinfo
.userid
!== undefined ) {
67 d
.resolve( { valid
: false, messages
: [ mw
.message( 'userexists' ).parseDom() ] } );
68 } else if ( !userinfo
.cancreate
) {
71 messages
: userinfo
.cancreateerror
? userinfo
.cancreateerror
.map( function ( m
) {
75 } else if ( userinfo
.name
!== username
) {
76 d
.resolve( { valid
: true, messages
: [
77 mw
.message( 'createacct-normalization', username
, userinfo
.name
).parseDom()
80 d
.resolve( { valid
: true, messages
: [] } );
85 return d
.promise( { abort
: apiPromise
.abort
} );
88 function checkPassword() {
89 // We could just use .then() if we didn't have to pass on .abort()…
93 if ( $usernameInput
.val().trim() === '' ) {
94 d
.resolve( { valid
: true, messages
: [] } );
98 apiPromise
= api
.post( {
99 action
: 'validatepassword',
100 user
: $usernameInput
.val(),
101 password
: $passwordInput
.val(),
102 email
: $emailInput
.val() || '',
103 realname
: $realNameInput
.val() || '',
106 errorsuselocal
: true,
107 uselang
: mw
.config
.get( 'wgUserLanguage' )
109 .done( function ( resp
) {
110 var pwinfo
= resp
.validatepassword
|| {};
113 valid
: pwinfo
.validity
=== 'Good',
114 messages
: pwinfo
.validitymessages
? pwinfo
.validitymessages
.map( function ( m
) {
121 return d
.promise( { abort
: apiPromise
.abort
} );
124 usernameChecker
= new mw
.htmlform
.Checker( $usernameInput
, checkUsername
);
125 usernameChecker
.attach();
127 passwordChecker
= new mw
.htmlform
.Checker( $passwordInput
, checkPassword
);
128 passwordChecker
.attach( $usernameInput
.add( $emailInput
).add( $realNameInput
) );