diff --git a/examples.html b/examples.html index 90113216..ff1fc055 100644 --- a/examples.html +++ b/examples.html @@ -132,6 +132,17 @@ availableTags: sampleTags, removeConfirmation: true }); + + //------------------------------- + // Regex enabled + //------------------------------- + $('#regexEnabled').tagit({ + regexEnable: true, + regexPattern: /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9]+(\.?[a-zA-Z0-9]+)?$/g, + onRegexFailure: function(evt, ui) { + $("#regexMessage").text("Wrong email!").fadeIn().delay(1000).fadeOut(); + } + }); }); @@ -256,6 +267,21 @@

Remove Confirmation

+
+

Regex enabled

+
+

+ With the option regexEnable you can set any regular expression in the regexPattern property to filter the input. +

+ +
+

+
+
+ diff --git a/js/tag-it.js b/js/tag-it.js index 078c264f..b045d720 100644 --- a/js/tag-it.js +++ b/js/tag-it.js @@ -39,6 +39,10 @@ // Used for autocomplete, unless you override `autocomplete.source`. availableTags : [], + regexPattern : null, // Holds the regex to be utilized when regexEnable is true + regexEnable : false, // Enables regex + regexObj : null, // To hold RegExp() object + // Use to override or add any options to the autocomplete widget. // // By default, autocomplete.source will map to availableTags, @@ -128,6 +132,10 @@ if (this.options.readOnly) this.tagInput.attr('disabled', 'disabled'); + if (this.options.regexEnable) { + this.options.regexObj = new RegExp(this.options.regexPattern); + } + if (this.options.tabIndex) { this.tagInput.attr('tabindex', this.options.tabIndex); } @@ -462,6 +470,14 @@ return false; } + if (this.options.regexEnable && value !== '') { + that.options.regexObj.lastIndex = 0; + if (!that.options.regexObj.test(value)) { + this._trigger('onRegexFailure', null, {duringInitialization: duringInitialization}); + return false; + } + } + if (this.options.tagLimit && this._tags().length >= this.options.tagLimit) { this._trigger('onTagLimitExceeded', null, {duringInitialization: duringInitialization}); return false; diff --git a/js/tag-it.min.js b/js/tag-it.min.js index fd6140c8..b430973d 100644 --- a/js/tag-it.min.js +++ b/js/tag-it.min.js @@ -1,17 +1 @@ -(function(b){b.widget("ui.tagit",{options:{allowDuplicates:!1,caseSensitive:!0,fieldName:"tags",placeholderText:null,readOnly:!1,removeConfirmation:!1,tagLimit:null,availableTags:[],autocomplete:{},showAutocompleteOnFocus:!1,allowSpaces:!1,singleField:!1,singleFieldDelimiter:",",singleFieldNode:null,animate:!0,tabIndex:null,beforeTagAdded:null,afterTagAdded:null,beforeTagRemoved:null,afterTagRemoved:null,onTagClicked:null,onTagLimitExceeded:null,onTagAdded:null,onTagRemoved:null,tagSource:null},_create:function(){var a= -this;this.element.is("input")?(this.tagList=b("").insertAfter(this.element),this.options.singleField=!0,this.options.singleFieldNode=this.element,this.element.addClass("tagit-hidden-field")):this.tagList=this.element.find("ul, ol").andSelf().last();this.tagInput=b('').addClass("ui-widget-content");this.options.readOnly&&this.tagInput.attr("disabled","disabled");this.options.tabIndex&&this.tagInput.attr("tabindex",this.options.tabIndex);this.options.placeholderText&&this.tagInput.attr("placeholder", -this.options.placeholderText);this.options.autocomplete.source||(this.options.autocomplete.source=function(a,e){var d=a.term.toLowerCase(),c=b.grep(this.options.availableTags,function(a){return 0===a.toLowerCase().indexOf(d)});this.options.allowDuplicates||(c=this._subtractArray(c,this.assignedTags()));e(c)});this.options.showAutocompleteOnFocus&&(this.tagInput.focus(function(b,d){a._showAutocomplete()}),"undefined"===typeof this.options.autocomplete.minLength&&(this.options.autocomplete.minLength= -0));b.isFunction(this.options.autocomplete.source)&&(this.options.autocomplete.source=b.proxy(this.options.autocomplete.source,this));b.isFunction(this.options.tagSource)&&(this.options.tagSource=b.proxy(this.options.tagSource,this));this.tagList.addClass("tagit").addClass("ui-widget ui-widget-content ui-corner-all").append(b('
  • ').append(this.tagInput)).click(function(d){var c=b(d.target);c.hasClass("tagit-label")?(c=c.closest(".tagit-choice"),c.hasClass("removed")||a._trigger("onTagClicked", -d,{tag:c,tagLabel:a.tagLabel(c)})):a.tagInput.focus()});var c=!1;if(this.options.singleField)if(this.options.singleFieldNode){var d=b(this.options.singleFieldNode),f=d.val().split(this.options.singleFieldDelimiter);d.val("");b.each(f,function(b,d){a.createTag(d,null,!0);c=!0})}else this.options.singleFieldNode=b(''),this.tagList.after(this.options.singleFieldNode);c||this.tagList.children("li").each(function(){b(this).hasClass("tagit-new")|| -(a.createTag(b(this).text(),b(this).attr("class"),!0),b(this).remove())});this.tagInput.keydown(function(c){if(c.which==b.ui.keyCode.BACKSPACE&&""===a.tagInput.val()){var d=a._lastTag();!a.options.removeConfirmation||d.hasClass("remove")?a.removeTag(d):a.options.removeConfirmation&&d.addClass("remove ui-state-highlight")}else a.options.removeConfirmation&&a._lastTag().removeClass("remove ui-state-highlight");if(c.which===b.ui.keyCode.COMMA&&!1===c.shiftKey||c.which===b.ui.keyCode.ENTER||c.which== -b.ui.keyCode.TAB&&""!==a.tagInput.val()||c.which==b.ui.keyCode.SPACE&&!0!==a.options.allowSpaces&&('"'!=b.trim(a.tagInput.val()).replace(/^s*/,"").charAt(0)||'"'==b.trim(a.tagInput.val()).charAt(0)&&'"'==b.trim(a.tagInput.val()).charAt(b.trim(a.tagInput.val()).length-1)&&0!==b.trim(a.tagInput.val()).length-1))c.which===b.ui.keyCode.ENTER&&""===a.tagInput.val()||c.preventDefault(),a.options.autocomplete.autoFocus&&a.tagInput.data("autocomplete-open")||(a.tagInput.autocomplete("close"),a.createTag(a._cleanedInput()))}).blur(function(b){a.tagInput.data("autocomplete-open")|| -a.createTag(a._cleanedInput())});if(this.options.availableTags||this.options.tagSource||this.options.autocomplete.source)d={select:function(b,c){a.createTag(c.item.value);return!1}},b.extend(d,this.options.autocomplete),d.source=this.options.tagSource||d.source,this.tagInput.autocomplete(d).bind("autocompleteopen.tagit",function(b,c){a.tagInput.data("autocomplete-open",!0)}).bind("autocompleteclose.tagit",function(b,c){a.tagInput.data("autocomplete-open",!1)}),this.tagInput.autocomplete("widget").addClass("tagit-autocomplete")}, -destroy:function(){b.Widget.prototype.destroy.call(this);this.element.unbind(".tagit");this.tagList.unbind(".tagit");this.tagInput.removeData("autocomplete-open");this.tagList.removeClass("tagit ui-widget ui-widget-content ui-corner-all tagit-hidden-field");this.element.is("input")?(this.element.removeClass("tagit-hidden-field"),this.tagList.remove()):(this.element.children("li").each(function(){b(this).hasClass("tagit-new")?b(this).remove():(b(this).removeClass("tagit-choice ui-widget-content ui-state-default ui-state-highlight ui-corner-all remove tagit-choice-editable tagit-choice-read-only"), -b(this).text(b(this).children(".tagit-label").text()))}),this.singleFieldNode&&this.singleFieldNode.remove());return this},_cleanedInput:function(){return b.trim(this.tagInput.val().replace(/^"(.*)"$/,"$1"))},_lastTag:function(){return this.tagList.find(".tagit-choice:last:not(.removed)")},_tags:function(){return this.tagList.find(".tagit-choice:not(.removed)")},assignedTags:function(){var a=this,c=[];this.options.singleField?(c=b(this.options.singleFieldNode).val().split(this.options.singleFieldDelimiter), -""===c[0]&&(c=[])):this._tags().each(function(){c.push(a.tagLabel(this))});return c},_updateSingleTagsField:function(a){b(this.options.singleFieldNode).val(a.join(this.options.singleFieldDelimiter)).trigger("change")},_subtractArray:function(a,c){for(var d=[],f=0;f=this.options.tagLimit)return this._trigger("onTagLimitExceeded",null,{duringInitialization:d}),!1;var g=b(this.options.onTagClicked?'':'').text(a),e=b("
  • ").addClass("tagit-choice ui-widget-content ui-state-default ui-corner-all").addClass(c).append(g); -this.options.readOnly?e.addClass("tagit-choice-read-only"):(e.addClass("tagit-choice-editable"),c=b("").addClass("ui-icon ui-icon-close"),c=b('\u00d7').addClass("tagit-close").append(c).click(function(a){f.removeTag(e)}),e.append(c));this.options.singleField||(g=g.html(),e.append(''));!1!==this._trigger("beforeTagAdded",null,{tag:e,tagLabel:this.tagLabel(e), -duringInitialization:d})&&(this.options.singleField&&(g=this.assignedTags(),g.push(a),this._updateSingleTagsField(g)),this._trigger("onTagAdded",null,e),this.tagInput.val(""),this.tagInput.parent().before(e),this._trigger("afterTagAdded",null,{tag:e,tagLabel:this.tagLabel(e),duringInitialization:d}),this.options.showAutocompleteOnFocus&&!d&&setTimeout(function(){f._showAutocomplete()},0))},removeTag:function(a,c){c="undefined"===typeof c?this.options.animate:c;a=b(a);this._trigger("onTagRemoved", -null,a);if(!1!==this._trigger("beforeTagRemoved",null,{tag:a,tagLabel:this.tagLabel(a)})){if(this.options.singleField){var d=this.assignedTags(),f=this.tagLabel(a),d=b.grep(d,function(a){return a!=f});this._updateSingleTagsField(d)}if(c){a.addClass("removed");var d=this._effectExists("blind")?["blind",{direction:"horizontal"},"fast"]:["fast"],g=this;d.push(function(){a.remove();g._trigger("afterTagRemoved",null,{tag:a,tagLabel:g.tagLabel(a)})});a.fadeOut("fast").hide.apply(a,d).dequeue()}else a.remove(), -this._trigger("afterTagRemoved",null,{tag:a,tagLabel:this.tagLabel(a)})}},removeTagByLabel:function(a,b){var d=this._findTagByLabel(a);if(!d)throw"No such tag exists with the name '"+a+"'";this.removeTag(d,b)},removeAll:function(){var a=this;this._tags().each(function(b,d){a.removeTag(d,!1)})}})})(jQuery); +!function(t){t.widget("ui.tagit",{options:{allowDuplicates:!1,caseSensitive:!0,fieldName:"tags",placeholderText:null,readOnly:!1,removeConfirmation:!1,tagLimit:null,availableTags:[],regexPattern:null,regexEnable:!1,regexObj:null,autocomplete:{},showAutocompleteOnFocus:!1,allowSpaces:!1,singleField:!1,singleFieldDelimiter:",",singleFieldNode:null,animate:!0,tabIndex:null,beforeTagAdded:null,afterTagAdded:null,beforeTagRemoved:null,afterTagRemoved:null,onTagClicked:null,onTagLimitExceeded:null,onTagAdded:null,onTagRemoved:null,tagSource:null},_create:function(){var e=this;this.element.is("input")?(this.tagList=t("").insertAfter(this.element),this.options.singleField=!0,this.options.singleFieldNode=this.element,this.element.addClass("tagit-hidden-field")):this.tagList=this.element.find("ul, ol").andSelf().last(),this.tagInput=t('').addClass("ui-widget-content"),this.options.readOnly&&this.tagInput.attr("disabled","disabled"),this.options.regexEnable&&(this.options.regexObj=new RegExp(this.options.regexPattern)),this.options.tabIndex&&this.tagInput.attr("tabindex",this.options.tabIndex),this.options.placeholderText&&this.tagInput.attr("placeholder",this.options.placeholderText),this.options.autocomplete.source||(this.options.autocomplete.source=function(e,i){var a=e.term.toLowerCase(),s=t.grep(this.options.availableTags,function(t){return 0===t.toLowerCase().indexOf(a)});this.options.allowDuplicates||(s=this._subtractArray(s,this.assignedTags())),i(s)}),this.options.showAutocompleteOnFocus&&(this.tagInput.focus(function(t,i){e._showAutocomplete()}),"undefined"==typeof this.options.autocomplete.minLength&&(this.options.autocomplete.minLength=0)),t.isFunction(this.options.autocomplete.source)&&(this.options.autocomplete.source=t.proxy(this.options.autocomplete.source,this)),t.isFunction(this.options.tagSource)&&(this.options.tagSource=t.proxy(this.options.tagSource,this)),this.tagList.addClass("tagit").addClass("ui-widget ui-widget-content ui-corner-all").append(t('
  • ').append(this.tagInput)).click(function(i){var a=t(i.target);if(a.hasClass("tagit-label")){var s=a.closest(".tagit-choice");s.hasClass("removed")||e._trigger("onTagClicked",i,{tag:s,tagLabel:e.tagLabel(s)})}else e.tagInput.focus()});var i=!1;if(this.options.singleField)if(this.options.singleFieldNode){var a=t(this.options.singleFieldNode),s=a.val().split(this.options.singleFieldDelimiter);a.val(""),t.each(s,function(t,a){e.createTag(a,null,!0),i=!0})}else this.options.singleFieldNode=t(''),this.tagList.after(this.options.singleFieldNode);if(i||this.tagList.children("li").each(function(){t(this).hasClass("tagit-new")||(e.createTag(t(this).text(),t(this).attr("class"),!0),t(this).remove())}),this.tagInput.keydown(function(i){if(i.which==t.ui.keyCode.BACKSPACE&&""===e.tagInput.val()){var a=e._lastTag();!e.options.removeConfirmation||a.hasClass("remove")?e.removeTag(a):e.options.removeConfirmation&&a.addClass("remove ui-state-highlight")}else e.options.removeConfirmation&&e._lastTag().removeClass("remove ui-state-highlight");(i.which===t.ui.keyCode.COMMA&&i.shiftKey===!1||i.which===t.ui.keyCode.ENTER||i.which==t.ui.keyCode.TAB&&""!==e.tagInput.val()||i.which==t.ui.keyCode.SPACE&&e.options.allowSpaces!==!0&&('"'!=t.trim(e.tagInput.val()).replace(/^s*/,"").charAt(0)||'"'==t.trim(e.tagInput.val()).charAt(0)&&'"'==t.trim(e.tagInput.val()).charAt(t.trim(e.tagInput.val()).length-1)&&t.trim(e.tagInput.val()).length-1!==0))&&((i.which!==t.ui.keyCode.ENTER||""!==e.tagInput.val())&&i.preventDefault(),e.options.autocomplete.autoFocus&&e.tagInput.data("autocomplete-open")||(e.tagInput.autocomplete("close"),e.createTag(e._cleanedInput())))}).blur(function(t){e.tagInput.data("autocomplete-open")||e.createTag(e._cleanedInput())}),this.options.availableTags||this.options.tagSource||this.options.autocomplete.source){var n={select:function(t,i){return e.createTag(i.item.value),!1}};t.extend(n,this.options.autocomplete),n.source=this.options.tagSource||n.source,this.tagInput.autocomplete(n).bind("autocompleteopen.tagit",function(t,i){e.tagInput.data("autocomplete-open",!0)}).bind("autocompleteclose.tagit",function(t,i){e.tagInput.data("autocomplete-open",!1)}),this.tagInput.autocomplete("widget").addClass("tagit-autocomplete")}},destroy:function(){return t.Widget.prototype.destroy.call(this),this.element.unbind(".tagit"),this.tagList.unbind(".tagit"),this.tagInput.removeData("autocomplete-open"),this.tagList.removeClass(["tagit","ui-widget","ui-widget-content","ui-corner-all","tagit-hidden-field"].join(" ")),this.element.is("input")?(this.element.removeClass("tagit-hidden-field"),this.tagList.remove()):(this.element.children("li").each(function(){t(this).hasClass("tagit-new")?t(this).remove():(t(this).removeClass(["tagit-choice","ui-widget-content","ui-state-default","ui-state-highlight","ui-corner-all","remove","tagit-choice-editable","tagit-choice-read-only"].join(" ")),t(this).text(t(this).children(".tagit-label").text()))}),this.singleFieldNode&&this.singleFieldNode.remove()),this},_cleanedInput:function(){return t.trim(this.tagInput.val().replace(/^"(.*)"$/,"$1"))},_lastTag:function(){return this.tagList.find(".tagit-choice:last:not(.removed)")},_tags:function(){return this.tagList.find(".tagit-choice:not(.removed)")},assignedTags:function(){var e=this,i=[];return this.options.singleField?(i=t(this.options.singleFieldNode).val().split(this.options.singleFieldDelimiter),""===i[0]&&(i=[])):this._tags().each(function(){i.push(e.tagLabel(this))}),i},_updateSingleTagsField:function(e){t(this.options.singleFieldNode).val(e.join(this.options.singleFieldDelimiter)).trigger("change")},_subtractArray:function(e,i){for(var a=[],s=0;s=this.options.tagLimit)return this._trigger("onTagLimitExceeded",null,{duringInitialization:a}),!1;var o=t(this.options.onTagClicked?'':'').text(e),l=t("
  • ").addClass("tagit-choice ui-widget-content ui-state-default ui-corner-all").addClass(i).append(o);if(this.options.readOnly)l.addClass("tagit-choice-read-only");else{l.addClass("tagit-choice-editable");var g=t("").addClass("ui-icon ui-icon-close"),r=t('×').addClass("tagit-close").append(g).click(function(t){s.removeTag(l)});l.append(r)}if(!this.options.singleField){var u=o.html();l.append('')}if(this._trigger("beforeTagAdded",null,{tag:l,tagLabel:this.tagLabel(l),duringInitialization:a})!==!1){if(this.options.singleField){var h=this.assignedTags();h.push(e),this._updateSingleTagsField(h)}this._trigger("onTagAdded",null,l),this.tagInput.val(""),this.tagInput.parent().before(l),this._trigger("afterTagAdded",null,{tag:l,tagLabel:this.tagLabel(l),duringInitialization:a}),this.options.showAutocompleteOnFocus&&!a&&setTimeout(function(){s._showAutocomplete()},0)}},removeTag:function(e,i){if(i="undefined"==typeof i?this.options.animate:i,e=t(e),this._trigger("onTagRemoved",null,e),this._trigger("beforeTagRemoved",null,{tag:e,tagLabel:this.tagLabel(e)})!==!1){if(this.options.singleField){var a=this.assignedTags(),s=this.tagLabel(e);a=t.grep(a,function(t){return t!=s}),this._updateSingleTagsField(a)}if(i){e.addClass("removed");var n=this._effectExists("blind")?["blind",{direction:"horizontal"},"fast"]:["fast"],o=this;n.push(function(){e.remove(),o._trigger("afterTagRemoved",null,{tag:e,tagLabel:o.tagLabel(e)})}),e.fadeOut("fast").hide.apply(e,n).dequeue()}else e.remove(),this._trigger("afterTagRemoved",null,{tag:e,tagLabel:this.tagLabel(e)})}},removeTagByLabel:function(t,e){var i=this._findTagByLabel(t);if(!i)throw"No such tag exists with the name '"+t+"'";this.removeTag(i,e)},removeAll:function(){var t=this;this._tags().each(function(e,i){t.removeTag(i,!1)})}})}(jQuery); \ No newline at end of file