Results 1 to 6 of 6

Thread: OnKeyPress Not called + JS

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    OnKeyPress Not called + JS

    Code:
     <asp:TextBox ID="TextBox1" runat="server" onKeyPress='return maxLength(this,"30");'></asp:TextBox>
    Code:
    function maxLength(field,maxChars)
     {
     alert('onKeyPress');
           if(field.value.length >= maxChars) {
              event.returnValue=false; 
              alert("more than " +maxChars + " chars");
              return false;
           }
    
     }
    When I Press any Character , maxLength function is not called,WHy SO?

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: OnKeyPress Not called + JS

    maxLength is a property of the Text Object. Browsers are assuming you want to access that property not call a function.

    Since Javascript is case sensitive, changing the function name to MaxLength or maxlength will work.

  3. #3
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: OnKeyPress Not called + JS

    agreed
    change the name of the function
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: OnKeyPress Not called + JS

    to add to what brucevde and avrail wrote, you need some kind of javascript debugger (firefox firebug is a very good one) when you work with javascript.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: OnKeyPress Not called + JS

    i use IE8 Developer tool,
    and you also should try Chrome Developer Tools it is perfect
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: OnKeyPress Not called + JS

    sonia.sardana,

    I am playing catch up on some threads here, been away on holiday...

    Did you get a solution to your problem? If so, can you remember to mark your thread as resolved?

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width