Count the number of occurrences in a string

Discussion in 'JavaScript' started by dargre, Mar 25, 2008.

  1. #1
    I'm trying to limit emoticons posted for comment message.
    It's easy for me to do that in PHP.
    But still can't find bugless function in javascript.

    Let's say I have an array of some emoticons codes, like ":sad:", etc.
    So everytime someone clicks over emot icon I wish to call javascript function to check if total number of emots code exceeded my limit.
    Then instead of putting code into textarea (have another good function for that) - display alert box if number exceeded limit.

    Sample how function should start...

    
    function check(object) {
       var limit=5;
       var content=document.getElementById(object).value;
       var smileys = new Array(':)',':)',':sad:',':mad:');   
       
       ....... how to retrieve number of smileys codes within content?
    }
    
    Code (markup):
     
    dargre, Mar 25, 2008 IP
  2. dargre

    dargre Peon

    Messages:
    1,007
    Likes Received:
    161
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Never mind.
    Just wrote function and tested it :). It's working.
    I used split method on textarea value.
     
    dargre, Mar 25, 2008 IP