Skip to content

Instantly share code, notes, and snippets.

@pagelab
Created September 21, 2021 01:15
Show Gist options
  • Save pagelab/096f207c213b83f61a55ddeb874361a0 to your computer and use it in GitHub Desktop.
Save pagelab/096f207c213b83f61a55ddeb874361a0 to your computer and use it in GitHub Desktop.
Validação de campo textarea utilizando o atributo pattern (não suportado nativamente).
// Validação dos campos para Javascript personalizado.
jQuery( '.epc-custom-js textarea' ).on( 'input change propertychange', function() {
var pattern = jQuery( this ).attr( 'pattern' );
// Define o padrão da expressão regular.
if ( typeof pattern !== typeof undefined && pattern !== false ) {
var patternRegex = new RegExp('^' + pattern.replace(/^\^|\$$/g, '') + '$', 'gm' );
hasError = ! jQuery( this ).val().match( patternRegex );
// Só apresenta a mensagem se o campo não estiver vazio.
if ( hasError && jQuery( this ).val().trim().length > 1 ) {
this.setCustomValidity( epico_i18n.textarea_error );
} else {
this.setCustomValidity( '' );
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment