Javascript Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veenna
    New Member
    • Dec 2007
    • 65

    #1

    Javascript Validation

    hi,

    i have 20 madatory text boxes in one page.and i want to do validation using javascript.For that i am calling following function in button click.

    Code:
    function validate()
    {
     var obj1 = document.getElementById('<%=txtMovie.ClientID%>').id; 
    
             for(var i=1;i<20;i++)
               {
                     var txtID = "obj" + i;
                   var txtBox= document.getElementById(txtID);
                     
                  if(txtID.value.length == 0)
                    {
                     alert("something");;
                       return false;           
                 }
              }	
    }
    But in this line i am not able to get the control
    Code:
    document.getElementById(txtID)
    .

    How can i do this??
    any help??

    Regards,
    Veena
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Show the corresponding HTML. Are you sure the IDs of the text boxes are "obj1", "obj2", etc.?

    Comment

    • veenna
      New Member
      • Dec 2007
      • 65

      #3
      hi acoder,

      thank u for reply.

      IDs of text boxes are different.In the above function i will be declaring obj1 ,obj2 as variables and i will assign ids to it.

      Code:
      var obj1 = document.getElementById('<%=txtMovie.ClientID%>');            
      var obj2 = document.getElementById('<%=txtCMovie.ClientID%>'); 
      var obj3 = document.getElementById('<%=txtNieMovie.ClientID%>'); 
      var obj4 = document.getElementById('<%=txtRelDate.ClientID%>'); 
      var obj5= document.getElementById('<%=txtLimDate.ClientID%>');

      So i want to find the control in foor loop.

      regards
      veena
      Last edited by Dormilich; Oct 14 '09, 01:23 PM. Reason: it's [code] tags, not [quote] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        In that case, try:
        Code:
        var txtBox = window["obj" + i];

        Comment

        • veenna
          New Member
          • Dec 2007
          • 65

          #5
          I tried with ur code but value is undefined.

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            Why can you get the objects by

            Code:
            var inpObj = document.getElementsByTagName('input');
            for(var i=0;i<inpObj.length;i++)
            {
               if(inpObj.type="text"||inpObj.type="TEXT")
                {
                     //do Validation
                }
            }
            Thanks and Regards
            Ramanan Kalirajan

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by RamananKaliraja n
              Code:
              if(inpObj.type="text"||inpObj.type="TEXT")
              this code won't work as intended...

              Code:
              for(var i=0;i<inpObj.length;i++)
              {
                 // note the == and the NodeList variable
                 if("text" == inpObj[i].type.toLowerCase())
                  {
                       //do Validation
                  }
              }

              Comment

              • veenna
                New Member
                • Dec 2007
                • 65

                #8
                But i cant use the above code because i have to display different messages based on text box id.

                thanks
                veena

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  the do something like
                  Code:
                  var message = {
                    id_1: "fool",
                    id_2: "idiot",
                    // and so on
                  };
                  // validation
                  alert(message[inpObj[i].id]);

                  Comment

                  Working...