Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('after_wcfm_articles_manage', function() {
- ?>
- <script>
- jQuery(function($) {
- function stripHTML(dirtyString) {
- return $("<div/>").html(dirtyString).text().trim();
- }
- $( document.body ).on( 'wcfm_form_validate', function(event, elem) {
- //Restrictions
- var MAX_CHARS_TITLE = 20;
- var MIN_CHARS_TITLE = 4;
- var TITLE_RESTRICTION_MSG = "Title must be between 4-20 characters";
- var MAX_CHARS_SHORT_DESCRIPTION = 200;
- var MIN_CHARS_SHORT_DESCRIPTION = 20;
- var SD_RESTRICTION_MSG = "Short description must be between 20-200 characters";
- var MAX_CHARS_DESCRIPTION = 500;
- var MIN_CHARS_DESCRIPTION = 50;
- var DESC_RESTRICTION_MSG = "Description must be between 50-500 characters";
- var MAX_NUM_TAGS = 10;
- var MIN_NUM_TAGS = 1;
- var TAGS_RESTRICTION_MSG = "Tag numbers must be between 1-10";
- var MAX_NUM_CATGS = 4;
- var MIN_NUM_CATGS = 1;
- var CATGS_RESTRICTION_MSG = "Category numbers must be between 1-4";
- var IMAGE_RESTRICTION_MSG = "Article image is mandatory";
- var $elem = $(elem);
- //article title restrictions
- if($elem.find('input#title').length) {
- var data_val = $elem.find('input#title').val();
- if(data_val.length > MAX_CHARS_TITLE || data_val.length < MIN_CHARS_TITLE) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + TITLE_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + TITLE_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- $elem.find('input#title').removeClass('wcfm_validation_success').addClass('wcfm_validation_failed');
- }
- }
- //article short description restrictions
- var excerpt = stripHTML( getWCFMEditorContent( 'excerpt' ) );
- if(excerpt.length > MAX_CHARS_SHORT_DESCRIPTION || excerpt.length < MIN_CHARS_SHORT_DESCRIPTION) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + SD_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + SD_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- }
- //article description restrictions
- var description = stripHTML( getWCFMEditorContent( 'description' ) );
- if(description.length > MAX_CHARS_DESCRIPTION || description.length < MIN_CHARS_DESCRIPTION) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + DESC_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + DESC_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- }
- //article tags restrictions
- if($elem.find('textarea#article_tags').length) {
- var tags = $elem.find('textarea#article_tags').val().trim();
- if(!tags || tags.split(',').length > MAX_NUM_TAGS || tags.length < MIN_NUM_TAGS) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + TAGS_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + TAGS_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- }
- }
- //article category restrictions
- if($elem.find('ul#article_cats_checklist').length) {
- var $catgs = $elem.find('ul#article_cats_checklist [type="checkbox"]:checked');
- if($catgs.length > MAX_NUM_CATGS || $catgs.length < MIN_NUM_CATGS) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + CATGS_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + CATGS_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- }
- }
- //article image restriction
- if($elem.find('input#featured_img').length) {
- if(!$elem.find('input#featured_img').val()) {
- if( $wcfm_is_valid_form ) {
- $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + IMAGE_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
- } else {
- $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + IMAGE_RESTRICTION_MSG );
- }
- $wcfm_is_valid_form = false;
- }
- }
- } );
- });
- </script>
- <?php
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement