Hi,
what’s the regex to validate
a list of tags
empty string // not valid
javascript //valid
javascript,php //valid
javascript,php, // not valid
,javascript // not valid
javascript,php // not valid
Thanks in advance.
Hi,
what’s the regex to validate
a list of tags
empty string // not valid
javascript //valid
javascript,php //valid
javascript,php, // not valid
,javascript // not valid
javascript,php // not valid
Thanks in advance.
Hi,
When you say tags, could you elaborate.
Like
javascript,html,css
I mean all the topics relate to a post
var re = /^\w(\s*,?\s*\w)*$/;
var s1 = ‘’; //
var s2 = ‘javascript’; //valid
var s3 = ‘javascript,php’; //valid
var s4 = ‘javascript,php,’; // not valid
var s5 = ‘,javascript’; // not valid
var s6 = ‘javascript,php’; // not valid
console.log(re.test(s1));
console.log(re.test(s2));
console.log(re.test(s3));
console.log(re.test(s4));
console.log(re.test(s5));
console.log(re.test(s6));
it seems to work
http://stackoverflow.com/questions/4316735/javascript-regex-for-comma-seperated-tags
Although the regex will probably work ok for you, it would be neater to split the string at the commas, then check the resultant array for the occurrence of an empty string.
function validate(s){
return (s.split(",").indexOf("") === -1)? "valid": "invalid";
}
console.log(validate('')); // invalid
console.log(validate('javascript')); // valid
console.log(validate('javascript,php')); // valid
console.log(validate('javascript,php,')); // invalid
console.log(validate(',javascript')); // invalid
console.log(validate('javascript,,php')); // invalid
Thanks for the snippet but I’m using angularjs and ng-pattern which do a real time validation
so it’s quite hard to integrate the code.
BTW why on earth the javascript forum have not added angularjs for his topic
javascript + jquery + angularjs
This is actually under discussion in the staff area at the moment.
Watch this space