Javascript validation Gridview FooterTemplate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • btreddy
    New Member
    • Oct 2008
    • 83

    Javascript validation Gridview FooterTemplate

    Hii Experts,

    im facing a problem with the javascript validation.i've a gridview in my web form and added a footer tempalte and placed textboxes in tht tempalte ,just to add a new row to the gridview and i wanna to validate the emalid entered in the footertextbox of the gridview. so i added a javasript function :
    Code:
     function OnCheckEmailID(srcID)
        {
           
           var email=document.getElementById(srcID);     
           var filter =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
           if (!filter.test(email.value)&& !email.value=="") 
           {         
              alert('Please provide a valid email address');
              email.value="";
              email.focus();   
              return false;            
           }
           else
            return true;
          
        }
    and i linked this function to the change event of my text box in the source code like this:
    Code:
    <asp:TextBox ID="txtdummy" runat="server" Width="150px" OnChange="OnCheckEmailID('ctl00_BodyContent_txtdummy')" ></asp:TextBox>
    But unfortunately this is not working ...but for testing purpose i placed one textbox outside the gridview and i linked the javascript function to the textchange event in the same ..thn its working fine...

    so the function is getting called for the textboxes outside the gridview..but not for the textboxes in the footer template of the gridview.

    can anybody tell me what im missing ..

    Thanks in Advance..
    Rgds,
    BTR.
    Last edited by Frinavale; Feb 10 '09, 02:03 PM. Reason: added [code] tags
  • btreddy
    New Member
    • Oct 2008
    • 83

    #2
    can anybody help me in finding the solution for this.

    Thanks

    Rgds,
    BTR.

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Isn't onchange the html input event through which to call JS events. Easiest way to get OnCheckEmailID working is therefore to change textbox to html input. Otherwise use server side code.

      Comment

      • btreddy
        New Member
        • Oct 2008
        • 83

        #4
        Thanks for your reply. everytime you are only replying to my queries .Thanks for your time.

        Its working now.

        But may i know the reason why its behaving like this..coz its working for other textboxes which are outside of the gridview.

        And when i try to call the JS function for the OnTextChanged instead of OnChange event of the server side Textbox i.e. <asp:Textbox> ,i got the runtime error..".too many characters in the literal".

        Requesting you kindly explain me whts going on behind the scenes.

        Rgds,
        BTR.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm not sure what's causing the problem but have you considered using a RegularExpressi onValidator instead?

          Add one to your footer template and see if it works...

          Comment

          • btreddy
            New Member
            • Oct 2008
            • 83

            #6
            Dear kenobewan,

            If you dont mind can you please tell me how can i do this validation on server side,instead of client side.like when i click on the Insert button which is there in the Footertemplate of the gridview it has to validate the footer textbox control's text and alert the user if he enters no value otherwise proceed for the insertion operation.

            As you know we dont have MessageBox facility in ASP.NET.

            Please Help me .

            Rgds,
            BTR.

            Comment

            • btreddy
              New Member
              • Oct 2008
              • 83

              #7
              Hii,

              I found that the ClientID( i.e.in the HTML source) of the textboxes which are there in the footer template of the Gridview is changing ,its not constant .and also its looks to be diffrent

              'ctl00_BodyCont ent_editgrid_ct l64_tbmail' this is the ID of a textbox with id=tbmail which is thr in the footertemplate;

              and for others the id is "ctl00_BodyCont ent_dvMeetingDe tail_txtorganiz ername"

              if we compare the both the diffrece is ,ct164 is added which is not constant and keep on changing.some times its 160 and sometimes its 162 and today its 164.

              Is this is the reason why its not geting the reference of the textbox controls which are placed in the footer row of a gridview.

              Kindly help me..

              Rgds,
              BTR.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Change your JavaScript to accept a TextBox as the parameter.


                So your function wouldn't change much....just pass it the TextBox element instead of the ID of the TextBox element:
                Code:
                function OnCheckEmailID(src)
                    {
                 
                       var email=src;     
                       var filter =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                       if (!filter.test(email.value)&& !email.value=="") 
                       {         
                          alert('Please provide a valid email address');
                          email.value="";
                          email.focus();   
                          return false;            
                       }
                       else
                        return true;
                 
                    }


                You would pass the function the TextBox like so:
                Code:
                <tdummy" runat="server" Width="150px" OnChange="OnCheckEmailID('this')" ></asp:TextBox>
                This would get around the problem with the changing ClientID.

                Comment

                • btreddy
                  New Member
                  • Oct 2008
                  • 83

                  #9
                  Dear Frinavale,

                  Thanks for the reply.

                  I changed the javascript function as you said ..but unfortunately its also not wroking.

                  and when i try to debug the javascript function in Visual studio 2005,wht i found is tht its not getting the footer textbox reference...but getting null value and its breaking immediatly..

                  I passes the name of the textbox as a parameter to the javascript function and chnged the function accrodingly ..thn also the same problem.

                  Really getting irritated with this strange behaviour..god knows wht's the problem is.

                  Rgds,
                  BTR.

                  Comment

                  • btreddy
                    New Member
                    • Oct 2008
                    • 83

                    #10
                    Dear kenobewan and Frinavale,

                    You both please help me...im in the middle of the ocean.

                    Rgds,
                    BTR.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      OK, well there's a problem with the code that I posted...

                      Passing "this" to the OnCheckEmailID function should not have quotations around it.

                      Try changing it to be:
                      Code:
                      <tdummy" runat="server" Width="150px" OnChange="OnCheckEmailID(this)" ></asp:TextBox>
                      If this doesn't work, post what you have now so that we can see what's going on.
                      (Are you using Ajax? Is your GridView in an update panel?)

                      Comment

                      • btreddy
                        New Member
                        • Oct 2008
                        • 83

                        #12
                        Dear Frinavale,

                        GoodMorning!!

                        Now it's working fine..Thank you Very much for your help and time.

                        I removed the quotes around "this"..its working fine.

                        Actually i forgot to tell you im using Ajax and the gridview is in the Updatepanel .

                        Actually my scenario is ,i've a button upon clicking on it, it has to display a modalpopup window which contains an update panel and inside that we've a detailsview and and gridview binded with respective datatable.

                        Tha same javascript function is working fine with the details view's textbox validation..but not with the gridview textboxes. :(.

                        Now upon changing the javascript function its working fine .

                        But still i am not getting what was wrong actually.

                        Anyhow ThankYou Very Much .

                        Rgds,
                        BTR.

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          The problem comes from using JavaScript functions along with Ajax.
                          There are ways to get around this (by registering your scripts properly with the ScriptManager and/or ScriptManagerPr oxy).

                          Comment

                          Working...