Click to See Complete Forum and Search --> : Help with improving this javascript.


mike55
04-20-2006, 06:20 AM
Hi all

I am involved in the development of a .NET web application that is capable of sending SMS and Email messages.

When sending an email, the character limit is currently being set to 640 characters.

When sending an SMS, a single SMS message can have a maximum of 160 characters. If the message is greater that 160 characters, it must be split over a number of messages, with the maximum number of characters per message being 153. Furthermore the maximum number of messages that you can send together is 4. Therefore for messages greater that 160 characters, the maximum number of characters that you can have is 612 characters.

I am using javascript to enforce the above constraints. Here it is:
**The limitField, is the field that the user is typing into.
The limitCount is the maximum number of charcters i.e. 640
The msgCount is the field where the number of sms messages is displayed, value is suppose to be between 1 and 4.
The charsCount is the field where the number of characters remaining is displayed.
The txtBox is a box that passes in a sponsors message.
The ddlMessage is used to determine what sort of message you are dealing with.

function limitText(limitField, limitCount, msgCount, charsCount, txtbox, selector){
var limitNum;
var userData = "";
var myData = limitField.value;
var data;

var dataChar = 0;
if (msgCount.value > 1) {
dataChar = 7 * msgCount.value;
}

if (!(txtbox.value == "")){
var sponsorMsg = " (Msg Sponsor " + txtbox.value + ")";
userData = limitField.value + sponsorMsg;
limitNum = 640 - sponsorMsg.length - dataChar;
} else{
userData = limitField.value;
limitNum = 640 - dataChar;
}

if (selector.value == "Email"){
data = limitField.value

var tempLength=0;

if (!(txtbox.value == "")){
var tempData = " (Msg Sponsor " + txtbox.value + ")"
tempLength = tempData.length;
}

charsCount.value = countForEmail(limitNum, userData);
data = myData.substr(0, (limitNum-tempLength))
msgCount = "1";

} else{

//debugger;
var messageSize = 160;
var charCount = userData.length;
var messageNo = calculateMsgNo(charCount);

if (messageNo > 1){

var dataCharacters = 7 * messageNo;
limitNum = limitNum - dataCharacters;
messageSize = 153;
limitCount.value = limitNum;
}

var charsRemaining = calculateCharactersRemaining(userData, messageSize);
var endCount = 0;

if (charsRemaining <= messageSize && messageNo == 5){

charsCount.value = "0";
msgCount.value = "4";
endCount = 4 * 153;

} else{

charsCount.value = charsRemaining;
msgCount.value = messageNo;
endCount = messageNo * messageSize;
}


if (!(txtbox.value == "")){
var tempData = " (Msg Sponsor " + txtbox.value + ")"
var tempLength = tempData.length;
data = myData.substr(0, (endCount - tempLength))
} else {
data = myData.substr(0, endCount);
}
}
limitField.value = data;
}


//Calculate the number of characters remaining in this message.
function calculateCharactersRemaining(userData, maxCharacters){

var result;
result = maxCharacters - (userData.length % maxCharacters);
return result;

}

function countForEmail(maxCharacters, userData){

var result;
var data;

result = maxCharacters - (userData.length);
return result;

}


function calculateMsgNo(message){

var result;

if (message <= 160) {

result = (message + 1) / 160;

} else if (message > 160){

result = (message + 1) / 153;

}

return myMax(Math.ceil(result), 1);
}


function myMax(anumber, another){
if (anumber > another){
return anumber;
}
else{
return another;
}
}


I know its not the nicest code, but it works to a degree. What happens is that when the user is typing in the textbox for the message, the above javascript is called on a keydown event.

The code works correctly if you go from start to finish without making any mistake.

The main problem occurs when after typing your message, you spot a mistake, you go back a click in front of the error and press the delete key to remove the mistake. The first character is deleted, however the curson returns to the end of the text, this is due to me using the .substr().

Ideally, I want to remove the .substr() from my code and replace it with something that just removes the last characters in the string, if the no. of characters is greater that the max limit.

Mike55.