|
1 | 1 | /* global alnp_settings_params */ |
2 | 2 | ( function( $, params ) { |
3 | | - var changed = false, |
4 | | - need_help = $('.need-help').data('tab'), |
5 | | - help_tabs = $('.contextual-help-tabs'), |
6 | | - help_contents = $('.contextual-help-tabs-wrap'), |
7 | | - panel = $('#' + $('#screen-meta-links').find('.show-settings').attr('aria-controls') ), |
8 | | - rtl = params.is_rtl; |
| 3 | + var changed = false, |
| 4 | + validated = true, |
| 5 | + $settings_form = $( 'form' ), |
| 6 | + need_help = $('.need-help').data('tab'), |
| 7 | + help_tabs = $('.contextual-help-tabs'), |
| 8 | + help_contents = $('.contextual-help-tabs-wrap'), |
| 9 | + panel = $('#' + $('#screen-meta-links').find('.show-settings').attr('aria-controls') ), |
| 10 | + rtl = params.is_rtl; |
9 | 11 |
|
10 | 12 | if ( rtl == 'rtl' ) { |
11 | 13 | rtl = true; |
|
60 | 62 | } |
61 | 63 | }); |
62 | 64 |
|
63 | | - // Clears any warnings previously set |
64 | | - $('.submit :input').click( function() { |
65 | | - changed = false; |
| 65 | + // Validate required fields. |
| 66 | + $settings_form.on('input validate change', 'input, number, email, textarea, select, checkbox, radio', function(e) { |
| 67 | + var $this = $( this ), |
| 68 | + required = $this.hasClass( 'required' ), |
| 69 | + validate_email = $this.hasClass( 'validate-email' ), |
| 70 | + event_type = e.type; |
| 71 | + |
| 72 | + if ( 'input' === event_type ) { |
| 73 | + $this.removeClass( 'validated' ); |
| 74 | + } |
| 75 | + |
| 76 | + if ( 'validate' === event_type || 'change' === event_type ) { |
| 77 | + |
| 78 | + if ( required ) { |
| 79 | + if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) { |
| 80 | + $this.removeClass( 'validated' ).addClass( 'invalid invalid-required-field' ); |
| 81 | + validated = false; |
| 82 | + } else if ( $this.val().length == 0 ) { |
| 83 | + $this.removeClass( 'validated' ).addClass( 'invalid invalid-required-field' ); |
| 84 | + validated = false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ( validate_email ) { |
| 89 | + if ( $this.val() ) { |
| 90 | + var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); |
| 91 | + |
| 92 | + if ( ! pattern.test( $this.val() ) ) { |
| 93 | + $this.removeClass( 'validated' ).addClass( 'invalid invalid-email' ); |
| 94 | + validated = false; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if ( validated ) { |
| 100 | + $this.removeClass( 'invalid invalid-required-field invalid-email' ).addClass( 'validated' ); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + // Clears any warnings previously set or warns the user of fields required. |
| 106 | + $('.submit :input').click( function(e) { |
| 107 | + if ( ! validated ) { |
| 108 | + e.preventDefault(); |
| 109 | + |
| 110 | + $.confirm({ |
| 111 | + icon: 'dashicons dashicons-warning', |
| 112 | + title: params.i18n_warning, |
| 113 | + content: 'There are required settings that need your attention before saving.', |
| 114 | + rtl: rtl, |
| 115 | + type: 'red', |
| 116 | + buttons: { |
| 117 | + close: { |
| 118 | + keys: ['n', 'esc', 'enter'], |
| 119 | + action: function () { |
| 120 | + } |
| 121 | + }, |
| 122 | + }, |
| 123 | + draggable: false, |
| 124 | + boxWidth: '500px', |
| 125 | + useBootstrap: false, |
| 126 | + }); |
| 127 | + |
| 128 | + } else { |
| 129 | + changed = false; |
| 130 | + } |
66 | 131 | }); |
67 | 132 |
|
68 | 133 | // Reset button - If pressed will warn the user that all settings will be reset if they continue. |
|
0 commit comments