diff --git a/js/formidable.js b/js/formidable.js index 4ce0e58566..f39e9e0c17 100644 --- a/js/formidable.js +++ b/js/formidable.js @@ -199,7 +199,7 @@ function frmFrontFormJS() { } } - validateFieldValue( field, errors ); + validateFieldValue( field, errors, true ); checkValidity( field, errors ); } } @@ -272,7 +272,7 @@ function frmFrontFormJS() { } if ( errors.length < 1 ) { - validateFieldValue( field, errors ); + validateFieldValue( field, errors, false ); } removeFieldError( $fieldCont ); @@ -283,24 +283,37 @@ function frmFrontFormJS() { } } - function validateFieldValue( field, errors ) { + /** + * Validates a field value. + * + * @since x.x Added `onSubmit` parameter. + * + * @param {HTMLElement} field Field input. + * @param {Object} errors Errors data. + * @param {boolean} onSubmit Is `true` if the form is being submitted. + */ + function validateFieldValue( field, errors, onSubmit ) { if ( field.type === 'hidden' ) { // don't validate } else if ( field.type === 'number' ) { checkNumberField( field, errors ); } else if ( field.type === 'email' ) { - checkEmailField( field, errors ); + checkEmailField( field, errors, onSubmit ); } else if ( field.type === 'password' ) { - checkPasswordField( field, errors ); + checkPasswordField( field, errors, onSubmit ); } else if ( field.type === 'url' ) { checkUrlField( field, errors ); } else if ( field.pattern !== null ) { checkPatternField( field, errors ); } + /** + * @since x.x Added `onSubmit` to the data. + */ triggerCustomEvent( document, 'frm_validate_field_value', { field: field, - errors: errors + errors: errors, + onSubmit: onSubmit }); } @@ -429,7 +442,38 @@ function frmFrontFormJS() { } } - function checkEmailField( field, errors ) { + /** + * Checks if the confirm field should be checked. + * + * @since x.x + * + * @param {HTMLElement} field Field input. + * @param {boolean} onSubmit Is `true` if the form is being submitted. + */ + function shouldCheckConfirmField( field, onSubmit ) { + if ( onSubmit ) { + // Always check on submitting. + return true; + } + + if ( 0 === field.id.indexOf( 'field_conf_' ) ) { + // Always check if it's the confirm field. + return true; + } + + return false; + } + + /** + * Check the email field for errors. + * + * @since x.x Added `onSubmit` parameter. + * + * @param {HTMLElement} field Field input. + * @param {Object} errors Errors data. + * @param {boolean} onSubmit Is `true` if the form is being submitted. + */ + function checkEmailField( field, errors, onSubmit ) { const fieldID = getFieldId( field, true ), pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i; @@ -438,11 +482,24 @@ function frmFrontFormJS() { errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' ); } - confirmField( field, errors ); + if ( shouldCheckConfirmField( field, onSubmit ) ) { + confirmField( field, errors ); + } } - function checkPasswordField( field, errors ) { - confirmField( field, errors ); + /** + * Check the password field for errors. + * + * @since x.x Added `onSubmit` parameter. + * + * @param {HTMLElement} field Field input. + * @param {Object} errors Errors data. + * @param {boolean} onSubmit Is `true` if the form is being submitted. + */ + function checkPasswordField( field, errors, onSubmit ) { + if ( shouldCheckConfirmField( field, onSubmit ) ) { + confirmField( field, errors ); + } } function confirmField( field, errors ) {