我使用NobleCount和以下代码计算字符:
$('#message').NobleCount('#messageInfo',{
max_chars: getMaxChars(),
on_negative: function(t_obj, char_area, c_settings, char_rem){
}
});
我想要一个像计数短信的功能,如果达到限制,接下来的160个字符用于第二个短信,依此类推。我可以使用on_update,on_positive,on_negative和block_negative等参数。
我尝试了使用模数的东西,但它不起作用。有什么想法吗?
答案 0 :(得分:40)
请注意,SMS比您指示的要复杂得多。
标准“160个字符”的短信使用strange 7-bit encoding覆盖大多数ASCII,一些欧洲口音,misc符号(如€),一些大写希腊字母(看起来不像罗马字符的那些)。 / p>
如果您的消息使用其他字符,则可以使用UCS-2(如UTF-16但仅限BMP)将其编码为Unicode,限制为70个字符。实际上,规范表示 UCS-2,但表情符号(非BMP Unicode)可以在SMS中发送,这意味着使用了UTF-16 ,在这种情况下每个表情符号必须“消耗”总共70个中的2个字符。
但有些语言可以使用"national language shift tables"使用替代 7位编码。最着名的是土耳其语,还有西班牙语,葡萄牙语和十种印度次大陆语言。这些是标准的新增功能。
即使在7位编码中,一些字符也被“转义”,这意味着它们“耗尽”了2个字符。在默认的7位编码中,它们是:{}[]\|^~€
。
如果您的消息超过160个字符,它可以使用“连接短信”,但随后会在每条消息中添加一个小标题,这意味着每个分段只有153个字符的空间。该标题有两种不同版本,大小不同,因此可能不是153个字符,而是152个(来自内存)。类似地,对于Unicode连接的SMS,小标题使每个段有67个字符。
祝你好运!
答案 1 :(得分:24)
首先,字符计数非常容易。您只需要在字符串上使用length
属性。要计算所需的SMS消息数量,您需要除以160并向上舍入(因为161个字符需要2条消息)。您的代码应该看起来像这样:
HTML:
<textarea name="message" value="" id="message"></textarea>
<p>
<span id="remaining">160 characters remaining</span>
<span id="messages">1 message(s)</span>
</p>
jQuery的:
$(document).ready(function(){
var $remaining = $('#remaining'),
$messages = $remaining.next();
$('#message').keyup(function(){
var chars = this.value.length,
messages = Math.ceil(chars / 160),
remaining = messages * 160 - (chars % (messages * 160) || messages * 160);
$remaining.text(remaining + ' characters remaining');
$messages.text(messages + ' message(s)');
});
});
答案 2 :(得分:11)
这是一个小插件。这是我的第一个jQuery插件,我免费赠送;) 你只需要启动它:
$('#smsText').smsArea();
HTML:
<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left
<textarea id="smsText"></textarea>
Javascript(2014年8月18日更新):
(function($){
$.fn.smsArea = function(options){
var
e = this,
cutStrLength = 0,
s = $.extend({
cut: true,
maxSmsNum: 3,
interval: 400,
counters: {
message: $('#smsCount'),
character: $('#smsLength')
},
lengths: {
ascii: [160, 306, 459],
unicode: [70, 134, 201]
}
}, options);
e.keyup(function(){
clearTimeout(this.timeout);
this.timeout = setTimeout(function(){
var
smsType,
smsLength = 0,
smsCount = -1,
charsLeft = 0,
text = e.val(),
isUnicode = false;
for(var charPos = 0; charPos < text.length; charPos++){
switch(text[charPos]){
case "\n":
case "[":
case "]":
case "\\":
case "^":
case "{":
case "}":
case "|":
case "€":
smsLength += 2;
break;
default:
smsLength += 1;
}
if(text.charCodeAt(charPos) > 127 && text[charPos] != "€") isUnicode = true;
}
if(isUnicode){
smsType = s.lengths.unicode;
}else{
smsType = s.lengths.ascii;
}
for(var sCount = 0; sCount < s.maxSmsNum; sCount++){
cutStrLength = smsType[sCount];
if(smsLength <= smsType[sCount]){
smsCount = sCount + 1;
charsLeft = smsType[sCount] - smsLength;
break
}
}
if(s.cut) e.val(text.substring(0, cutStrLength));
smsCount == -1 && (smsCount = s.maxSmsNum, charsLeft = 0);
s.counters.message.html(smsCount);
s.counters.character.html(charsLeft);
}, s.interval)
}).keyup()
}}(jQuery));
DEMO:http://jsfiddle.net/t32h0gj4/1/
注意:有一些基本选项
$('#smsText').smsArea({cut:false}); //Do not cut the SMS
$('#smsText').smsArea({maxSmsNum:2}); //2 SMS Max
答案 3 :(得分:3)
一旦消息被解码为Craig McQueen在这篇帖子中说明并且你有实际的字符数,那么计算所需的短信量就足够了:
function cntsms(len){ return len<=0? 0: (len>160? Math.ceil(len/153): 1); }
......我非常喜欢单行解决方案。
答案 4 :(得分:2)
我认为以下脚本可以更准确地计算SMS部分:
//field: a text box that contains the SMS Text
//cntField: a text box that will contain the remaining count of characters for each part
//smsCntfield: a text box that will contain the count of parts
//lang: 0 for English, 2 for Arabic
//maxLimit: Maximum count of characters to limit the TextBox, (ex: for 5 SMS in Arabic 331, in English 762
function textCounter(field, cntfield, smsCntfield, lang, maxlimit) {
part1Count = 0;
part2Count = 0;
part3Count = 0;
part4Count = 0;
part5Count = 0;
if (lang == 2) {
// Arabic
part1Count = 70;
part2Count = 63;
part3Count = 66;
part4Count = 66;
part5Count = 66;
} else if (lang == 0) {
// English
part1Count = 160;
part2Count = 145;
part3Count = 152;
part4Count = 152;
part5Count = 152;
}
smsCount = 0;
smsCharCnt = 0;
smsTotalCount = 0;
if (field.value.length <= part1Count) {
smsCount = 1;
smsCharCnt = part1Count;
smsTotalCount = part1Count;
} else if (field.value.length <= (part1Count + part2Count)) {
smsCount = 2;
smsCharCnt = part2Count;
smsTotalCount = (part1Count+part2Count);
} else if (field.value.length <= (part1Count+part2Count+part3Count)) {
smsCount = 3;
smsCharCnt = part3Count;
smsTotalCount = (part1Count+part2Count+part3Count);
} else if (field.value.length <= (part1Count+part2Count+part3Count+part4Count)) {
smsCount = 4;
smsCharCnt = part4Count;
smsTotalCount = (part1Count+part2Count+part3Count+part4Count);
} else if (field.value.length <= (part1Count+part2Count+part3Count+part4Count+part5Count)) {
smsCount = 5;
smsCharCnt = part5Count;
smsTotalCount = (part1Count+part2Count+part3Count+part4Count+part5Count);
}
if (field.value.length > maxlimit) {
// if too long...trim it!
field.value = field.value.substring(0, maxlimit);
} else {
cntfield.value = smsTotalCount - field.value.length;
smsCntfield.value = smsCount;
}
}
使用示例:
<html:textarea cols="30" rows="5" property="textEn"
title="Text English"
onkeydown="textCounter(document.form.textEn,document.form.remLen2,document.form.smsCount2,0,762)"
onkeyup="textCounter(document.form.textEn,document.form.remLen2,document.form.smsCount2,0,762)" />
<br>
<input type="text" readonly="readonly" name="remLen2"
size="3" maxlength="3" value="160"
title="Char Count">
(
<input type="text" readonly="readonly" name="smsCount2"
size="1" maxlength="1" value="1"
title="SMS Parts' />">
)
答案 5 :(得分:2)
扩展表(GSM扩展字母表)中的每个字符都由两个字符表示,实际最大长度动态计算为:160 - k,其中k是消息中使用的扩展字符数。
当使用8位参考号连接消息时,您还必须考虑消息长度为153个字符。
Unicode长度为70个字符,连接63个字符。
答案 6 :(得分:1)
基于这里的问题,我尝试编写更好的解决方案。这里有几个最佳答案。
的Javascript
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function(){
part1Count = 160;
part2Count = 145;
part3Count = 152;
$('#message').keyup(function(){
var chars = $(this).val().length;
messages = 0;
remaining = 0;
total = 0;
if (chars <= part1Count) {
messages = 1;
remaining = part1Count - chars;
} else if (chars <= (part1Count + part2Count)) {
messages = 2;
remaining = part1Count + part2Count - chars;
} else if (chars > (part1Count + part2Count)) {
moreM = Math.ceil((chars - part1Count - part2Count) / part3Count) ;
remaining = part1Count + part2Count + (moreM * part3Count) - chars;
messages = 2 + moreM;
}
$('#remaining').text(remaining);
$('#messages').text(messages);
$('#total').text(chars);
if (remaining > 1) $('.cplural').show();
else $('.cplural').hide();
if (messages > 1) $('.mplural').show();
else $('.mplural').hide();
if (chars > 1) $('.tplural').show();
else $('.tplural').hide();
});
$('#message').keyup();
});
</script>
HTML
<textarea name="message" value="" id="message"></textarea>
<div>
<div><span id="remaining">160</span> Character<span class="cplural">s</span> Remaining</div>
<div>Total <span id="messages">1</span> Message<span class="mplural">s</span> <span id="total">0</span> Character<span class="tplural">s</span></div>
</div>
您可以在此处获取代码http://www.mindrestingplace.com/2012/07/18/sms-character-counter/
希望这能解决您的问题。 感谢
答案 7 :(得分:0)
我更新了脚本(为了我的目的),也将主题包含在计数中。这就是我想出的:
$(document).ready(function(){
var $remaining = $('#remaining'),
$messages = $remaining.next();
$('#message').keyup(function(){
var chars = this.value.length + document.getElementById('subject').value.length,
messages = Math.ceil(chars / 160),
remaining = messages * 160 - (chars % (messages * 160) || messages * 160);
$remaining.text(remaining);
//$remaining.text(remaining + ' characters remaining');
//$messages.text(messages + ' message(s)');
});
$('#subject').keyup(function(){
var chars = this.value.length + document.getElementById('message').value.length,
messages = Math.ceil(chars / 160),
remaining = messages * 160 - (chars % (messages * 160) || messages * 160);
$remaining.text(remaining);
//$remaining.text(remaining + ' characters remaining');
//$messages.text(messages + ' message(s)');
});
});
我测试了它的确有效。我也改了一下html:
<p>
<span id="remaining">160</span> characters remaining
<!--<span id="messages">1 message(s)</span>-->
</p>
答案 8 :(得分:0)
这是游戏的后期,但这是我目前正在组合的内容。
字符限制设置为459以允许连接的消息(由我的服务提供商设置),每使用160个字符的文本单元总计。请记住,这个方面是不完整的,因为由于消息标题和诸如此类的原因,每个单元在初始单元之后将包含少于160个字符的模板。我还没有从提供商那里得到确切的细分。
此脚本的关键区别在于字符数对非标准GSM字符敏感,它们的ascii值在包含的数组中指定。如果键入非标准字符,则计数为2,否则为1。
$('#sms-message').keyup(function(){
var chars = $(this).val(),
arr_chars = chars.split(''),
remaining = $('#remaining'),
messages = $('#messages'),
count = 0;
$.each(arr_chars, function(i, l){
var ascii = l.charCodeAt(0),
ascii_val = parseInt(ascii),
//array of special chars
arr = [13, 47, 92, 123, 124, 125, 126, 128];
if($.inArray(ascii_val, arr) !== -1) { count = count + 2; }
else { count = count + 1; }
});
//inaccurate count, will have to be finetuned to provider specs
var units = Math.ceil(count / 160),
remaining_chars = 459 - count;
remaining.text(remaining_chars + ' characters remaining');
messages.text(units + ' text unit(s)');
if(remaining_chars < 0) {
$(remaining).css('color', 'red');
}
else {
$(remaining).css('color', 'black');
}
});
答案 9 :(得分:0)
等等......
function cntsms(len) {
return len <= 0 ? 0 : ( len > 160 ? Math.ceil((len-7)/153) : 1);
}
答案 10 :(得分:0)
$(function() {
$('#message').keydown(function() {
var mychars = $('#message').val().length;
var mysms = Math.ceil(mychars / 160);
console.log (mysms);
console.log (mychars +'' + ' characters');
});
});