javascript - Jquery check if special char exists in text value -


i need check if textarea contains special characters, need count them 2 (sms lenght).

i wrote piece of code seems doesn't find no special chars, if write "€€€"

could please me? if rewrite directly function, without problem. thank tou!

var special_chars   = array('€', '%');  function charused() {     var count                   = $('#text').val().length;     var chars                   = $('#text').val().split("");     var numberofspecialchars    = 0;     if ($.inarray(chars, special_chars) > -1) {         numberofspecialchars++;     }     return count + numberofspecialchars; } // function 

a rewrite :

var nbspecialchars = $('#text').val().split(/[€%]/).length - 1; 

the idea make array of strings, using special characters separator :

'some € chars %' => ["some ", " chars ", ""] 

and use length of array deduce count of chars. there many other (faster) solutions 1 short.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -