Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions js/formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function frmFrontFormJS() {
}
}

validateFieldValue( field, errors );
validateFieldValue( field, errors, true );
checkValidity( field, errors );
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ function frmFrontFormJS() {
}

if ( errors.length < 1 ) {
validateFieldValue( field, errors );
validateFieldValue( field, errors, false );
}

removeFieldError( $fieldCont );
Expand All @@ -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
});
}

Expand Down Expand Up @@ -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 ) {
Comment thread
Crabcyborg marked this conversation as resolved.
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;

Expand All @@ -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 ) {
Expand Down