Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Javascript Overwriting current text values while checking for maximum character lenght

Avatar of TheUndecider
TheUndeciderFlag for United States of America asked on
JavaScript
3 Comments1 Solution1000 ViewsLast Modified:
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!






ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.LeeFlag of Malaysia imageDigital Marketing (Hospitality)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers