Improving javascript.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I have an application that is responsible for sending sms messages and email messages. Now the maximum number of characters allowed is 640, however the following constraints apply for sms messages.
1. if the sms message is greater than 160 characters it must be split into two messages, and each message must loose 7 characters which will be used for package markers. Therefore for a sms the maximum number of characters is 4 messages * 153 = 612 characters.

I have the following javascript that fires when the txtbox for the message is selected and a key on the keyboard is pressed. 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.
Code:
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;
  }
}

The code does what it has to do. i.e. limit the number of characters. The problem is that the code also executes when the directional keys are moved about and if you click the Delete key in the middle of a sentance, resulting in the cursor being sent to the very end of the text.

I would appreciate it if anyone could point out ways of improving this javascript, so that it doesn't cause the cursor to jump all over the place.

Mike55
 
Back
Top