$(document).ready(function() {
$("#testone").change(function() {
cleanTags($(this).attr('id'));
});
$('#testone').keyup(function(e) {
var tagsInput = $(this).val();
var tagsString = tagsInput.replace(/[ ,]+/g, ",");
$(this).val(tagsString);
});
$('#testone').keypress(function(e) {
var regex = new RegExp("^[ a-zA-Z]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
e.preventDefault();
return false;
});
});
/**
* Format the Tags Field text values
* Remove Duplicates
* Remove leading and trailing blanks, extra commas, etc.
*/
function cleanTags(idToClean) {
var stringToClean = jQuery.trim($('#'+idToClean).val());
// If there is no text entered then don't continue
if (stringToClean.length < 1) {
return (false);
}
// Replace characters that we dont want
stringToClean = stringToClean.replace(/[,]{2,}/g, ',').replace(/%/g, '').replace(/_/g, '').replace(/\r\n|\n|\r/g, ",");
// Split string to an array at every comma
var tempArr = stringToClean.split(',');
var uniqueArray = new Array();
// Search for duplicates, if found delete em
for (var i = 0; i < tempArr.length; i++) {
tempArr[i] = jQuery.trim(tempArr[i]);
if (tempArr[i] == '') {
tempArr.splice(i, 1);
i--;
}
if (uniqueArray[tempArr[i]] != undefined) {
tempArr.splice(i, 1);
i--;
}
uniqueArray[tempArr[i]] = '';
}
$('#'+idToClean).val(tempArr.join(','))
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.