Hello,
I am working on an ASP page that has some javascript validation functions.
I have the following formula in the textboxes onkeypress value.
//checking that the textboxes input doesn't exceed the maximum allowed characters
function TestMaxlength(strNm,strtxtBox, MaxChr, e)
{
var key
if (window.event)
key = event.keyCode;
else
key = e.which;
var MaxTxt = MaxChr + 1
var strTxt=document.form1.elements[strtxtBox].value
if ( key == 8 )
return;
else
if(strTxt.length > MaxChr)
{
if (window.event) //IE
window.event.returnValue = null;
else //Firefox
e.preventDefault();
alert(strNm + " are limited to " + MaxTxt + " characters!");
MaxTxt = 0
}
}
Where:
strNm = the textbox field type i.e. "Last Names"
strtxtBox = the actual textbox name i.e. "txtLastName"
MaxChr = the maximum number of characters for this text box i.e. "25"
e = event
My problem is that if I am tabbing and I need to udpate the existing value of a field where such value has already the max number of characters (like the State =2 or Zip Code = 5) I can't overwrite the existing value by just typing the new state. I need to press the backspace or the delete key first to delete the current value. Otherwise, I get my alert, i.e. "States are limited to 2 characters" because the 2 characters are already there.
I need to be able to start typing and overwriting the existing values if the text is highlighted even if the value is already the max number of characters. I'm assuming I need to check whether the current value is highlighted first and if it is, then I am supposed to delete it at the first key stroke (regardless if the first key is the backspace or the delete key).
Any ideas?
Thanks!