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
Post a Comment