Created
September 21, 2021 01:15
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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