SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [regex] javascript list of tags comma separated value validation

    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.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    When you say tags, could you elaborate.

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    When you say tags, could you elaborate.

    Like
    javascript,html,css

    I mean all the topics relate to a post

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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/4...seperated-tags

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    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.

    Code:
    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

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    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.

    Code:
    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

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by whisher View Post
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •