Javascript and forms

Rate It (1)

Last post 05-16-2008 10:33 AM by NC01. 8 replies.

Sort Posts:

  • Javascript and forms

    05-13-2008, 11:00 AM
    • Loading...
    • kashwmu
    • Joined on 10-08-2007, 3:41 PM
    • Posts 101

    Hi, i have a form that i am trying to use some javascript to validate the items on the client side to keep things quick for the user. I am also using master pages. I can not seem to get the javascript to work with anything on the page. if i add it to the OnClientClick it does not work. I have never done this before and was wondering if anybody could help me out. Thanks.

  • Re: Javascript and forms

    05-13-2008, 12:23 PM
    • Loading...
    • allanhorwitz
    • Joined on 04-10-2008, 1:45 PM
    • Philadelphia, PA
    • Posts 417

    Can you be more specific about what the Javascript is being used for? There may be inherit web controls that will do what you need for you.

    Allan Horwitz
  • Re: Javascript and forms

    05-13-2008, 1:17 PM
    • Loading...
    • kashwmu
    • Joined on 10-08-2007, 3:41 PM
    • Posts 101

    There may be controls that do that but that is not what i am asking, i am checking a textbox to make sure that there is a number in the box that is between the numbers that i specify. I am trying to learn some javascript for some client side scripting and would like to be able to understand how the things are working. Thanks

  • Re: Javascript and forms

    05-13-2008, 1:21 PM
    • Loading...
    • allanhorwitz
    • Joined on 04-10-2008, 1:45 PM
    • Philadelphia, PA
    • Posts 417

    kashwmu:

    There may be controls that do that but that is not what i am asking, i am checking a textbox to make sure that there is a number in the box that is between the numbers that i specify. I am trying to learn some javascript for some client side scripting and would like to be able to understand how the things are working. Thanks

    Okay, would you mind posting the code you have so far?

    Allan Horwitz
  • Re: Javascript and forms

    05-13-2008, 2:34 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,509
    • TrustedFriends-MVPs

    Your problem is probably with the IDs of the controls. A content holder (like a Master Page) mangles the ID of the control like content-holder-ID + "_" + control-ID which means OnClientClick('control-ID') won't work. Changing to OnClientClick('<%= control-ID.ClientID %>') will usually fix the problem.

    NC...

  • Re: Javascript and forms

    05-14-2008, 9:29 AM
    • Loading...
    • kashwmu
    • Joined on 10-08-2007, 3:41 PM
    • Posts 101

    To me it seems very simple but it is still not working right, i got it to do some of the stuff but here is the code.

    function validateNumber(field, msg, min, max){

    if (!min) {min = 0}

    if (!max) {max = 255} if ( (parseInt(field.value) != field.value) ||

    field.value.length < min || field.value.length > max){

    alert(msg);

    field.focus();

    field.select();

    return false;

    }

    return true;

    }

    It is telling me that field.focus() and field.select() are not availible. I am using vs 2008 which shows some of the items that javascript could have(from what i understand) and it does not include them. It also does not include field.value or anything like that. I found the code somewhere and am just trying to get it to work. Thanks.

  • Re: Javascript and forms

    05-15-2008, 11:49 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,509
    • TrustedFriends-MVPs

    How are you calling validateNumber(field, msg, min, max) and from what type of element? Give an example.

    NC...

  • Re: Javascript and forms

    05-15-2008, 3:31 PM
    • Loading...
    • kashwmu
    • Joined on 10-08-2007, 3:41 PM
    • Posts 101

    Sorry about not including that, my bad.

    I have tried from the main form with a submit and as of right now i am just trying to use a button until i can get it to work.

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validateNumber(this.txtshift,'This Works',1,3);" UseSubmitBehavior="False" />

    Thanks

  • Re: Javascript and forms

    05-16-2008, 10:33 AM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 9,509
    • TrustedFriends-MVPs

    When calling the validateNumber function this way
         OnClientClick="return validateNumber(this.txtshift,'This Works',1,3);"
    this.txtshift does not refer to a TextBox txtshift, it refers instead to a property of the Button named txtshift.

    Try it this way:
         1. Remove the OnClientClick event handler from the Designer (HTML view).
              <asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="False" />
         2. In the CodeBehind, Page_Load event handler, add this:
              Button1.OnClientClick = "return validateNumber('" + txtshift.ClientID + "','This Works',1,3);"
         3. Change your function as follows:

    function validateNumber(fieldId, msg, min, max)
    {
     var field = document.GetElementById(fieldId);

     if (!min)
     {
      min = 0;
     }

     if (!max)
     {
      max = 255;
     }

     if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max )
     {
      alert(msg);
      field.focus();
      field.select();

      return false;
     }

     return true;
    }

    NC...

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter