@@ -31,6 +31,34 @@ $(function() {
3131 } ) ;
3232
3333 watchPasswordStrength ( $ ( '#infosPassword' ) ) ;
34+
35+ // Prevent form submit when password does not meet minimum strength (score) or length
36+ $ ( '#mainForm' ) . on ( 'submit' , function ( e ) {
37+ const $passwordInput = $ ( '#infosPassword' ) ;
38+ if ( $passwordInput . length === 0 ) {
39+ return ;
40+ }
41+ const passwordValue = $passwordInput . val ( ) ;
42+ if ( ! passwordValue ) {
43+ return ;
44+ }
45+ const minScore = $passwordInput . data ( 'minscore' ) ;
46+ const minLength = $passwordInput . data ( 'minlength' ) ;
47+ const maxLength = $passwordInput . data ( 'maxlength' ) ;
48+ const result = zxcvbn ( passwordValue ) ;
49+ const scoreValid = result . score >= minScore ;
50+ const lengthValid = passwordValue . length >= minLength && passwordValue . length <= maxLength ;
51+ if ( ! scoreValid || ! lengthValid ) {
52+ e . preventDefault ( ) ;
53+ e . stopImmediatePropagation ( ) ;
54+ const errorMessage = $passwordInput . closest ( '.field-password' ) . data ( 'passwordMustBeStrong' )
55+ || 'The password must be strong (see requirements above).' ;
56+ $passwordInput . closest ( '.field-password' ) . find ( '.js-password-client-error' ) . show ( ) . text ( errorMessage ) ;
57+ $passwordInput . closest ( '.field-password' ) . find ( '.password-strength-feedback' ) . removeClass ( 'd-none' ) ;
58+ $ ( '#btNext' ) . show ( ) ;
59+ return false ;
60+ }
61+ } ) ;
3462} ) ;
3563
3664function checkTimeZone ( elt )
@@ -79,7 +107,7 @@ function in_array(needle, haystack) {
79107 */
80108function watchPasswordStrength ( element ) {
81109 element . on ( 'keyup' , function checkPasswordStrength ( ) {
82- $ ( '.field-password .errorTxt ' ) . hide ( ) ;
110+ $ ( '.field-password .js-password-client-error ' ) . hide ( ) ;
83111 const passwordValue = $ ( this ) . val ( ) ;
84112 const popoverElement = $ ( '.field-password .popover' ) ;
85113 let $feedbackContainer = $ ( this ) . parent ( ) . find ( '.password-strength-feedback' ) ;
0 commit comments