Unable to validate input data in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas251074
    New Member
    • Dec 2007
    • 198

    Unable to validate input data in array

    I am using ASP
    In my company, all employees have to sent materials out of premises. Any employee who need to sent material out will use this web application. In the first page, an employee enters firm name, vehicle no, driver name, purpose, persons taking out, received by, remarks. In the second page, employee enters no. of materials to be taken out. In the third page, displays no. of rows for entry of material details like material name, serial_no., qty, weight remarks.
    I have written validation for all field. All validation works well. In the third page, validation works well when no. of material taken out is 1. When it is > 1, then no validation works.

    Please suggest me, what to do?

    I am giving you code for entry and validation.

    Thanks and regards,
    Vikas
    Code:
     
    <html>
    <title>Material Entry Page</title>
    <script language="javascript">
     
    function check1(s)
    {
    var t
    var str=s
    len=str.length
    for(i=0;i<=len;i++)
    {
    if(str.charAt(i)==0 ||str.charAt(i)==1 ||str.charAt(i)==2 ||str.charAt(i)==3 ||str.charAt(i)==4 ||str.charAt(i)==5 ||str.charAt(i)==6 ||str.charAt(i)==7 ||str.charAt(i)==8 ||str.charAt(i)==9 )
    { 
    continue;
    }
    else
    {
    t=2
    return t;
    }
    }
    return 0;
    }
    function check(s)
    {
    var t
    var str=s	
    len=str.length
    if (len==0)
    {
    t=1
    return t;
    }
    else
    {
    for(i=0;i<=len;i++)
    {
    if(str.charAt(i)==0 ||str.charAt(i)==1 ||str.charAt(i)==2 ||str.charAt(i)==3 ||str.charAt(i)==4 ||str.charAt(i)==5 ||str.charAt(i)==6 ||str.charAt(i)==7 ||str.charAt(i)==8 ||str.charAt(i)==9 )
    {
    t=2
    return t;
    }
    else
    {
    continue;
    }
    }
    return 0;
    }
    }
    function check2(s)
    {
    var t
    var str=s	
    len=str.length
    if (len==0)
    {
    t=1
    return t;
    }
    else
    {
    for(i=0;i<=len;i++)
    {
    if(str.charAt(i)=="+" ||str.charAt(i)=="-" ||str.charAt(i)=="*"|| str.charAt(i)=="`" ||str.charAt(i)=="!" ||str.charAt(i)=="@" ||str.charAt(i)=="#" ||str.charAt(i)=="$" ||str.charAt(i)=="%" ||str.charAt(i)=="^" ||str.charAt(i)=="&" ||str.charAt(i)=="=" ||str.charAt(i)=="?" ||str.charAt(i)=="<" ||str.charAt(i)==">")
    {
    t=2
    return t;
    }
    else
    {
    continue;
    }
    }
    return 0;
    }
    }
    function checkmat()
    {
    var t,t1;
    var str=document.myform.mat.value
    t=check2(str);
    if(t==2)
    {
    document.myform.mat.select()
    alert("Special characters are not allowed.");
    }
    if(t==1)
    {
    document.myform.mat.select()
    alert("Material must be entered.");
    }
    }
    function checkcsno()
    {
    var str=document.myform.csno.value
    len=str.length
    if (len==0)
    {
    document.myform.csno.select()
    alert("Company serial number must be entered.")
    }
    }
    function checkdsno()
    {
    var str=document.myform.dsno.value
    len=str.length
    if (len==0)
    {
    document.myform.dsno.select()
    alert("Department serial number must be entered.")
    }
    }
    function checkqty()
    {
    var t,t1;
    var str=document.myform.qty.value
    t=check1(str);
    if(t==2)
    {
    document.myform.qty.select()
    alert("Please enter numbers only.")
    }
    if(str==0)
    {
    document.myform.qty.select()
    alert("Quantity must > 0.")
    }
    }
    function checkrem()
    {
    var t,t1;
    var str=document.myform.rem.value
    t=check2(str);
    if(t==2)
    {
    document.myform.rem.select()
    alert("Special characters are not allowed.")
    }
    }
    </script>
     
     
    <%
    Set CON = Server.CreateObject("ADODB.Connection")
    con.open "Provider=MSDAORA.1;dsn=ops;Password=op;User ID=outpass;Data Source=intradb;Persist Security Info=True"
    SET RS=SERVER.CREATEOBJECT("ADODB.RECORDSET")
    'RS=CON.EXECUTE("select max(gp_no) from gps")
    'gn=rs("max(gp_no)")
    gn=session("gno")
    %>
    <!-- #include file="Hdr.asp" -->
    <form name="myform" action="smaterial.asp" METHOD="POST">
     
    <b>Gatepass Number : <font color="blue"><%=GN%></font> <input type="hidden" name="gno" value="<%=GN%>" >
    <br><br>
    <TABLE border=3 cellPadding=5 cellSpacing=5 width="80%">
    <tr>
    <TD align='center'><B>SNo.</B></TD> 
    <TD align='center'><B>Material Name</B></TD> 
    <TD align='center'><B>Company Serial Number</B></TD> 
    <TD align='center'><B>Department Serial Number</B></TD>
    <TD align='center'><B>Quantity<br>
    (Number only)</B></TD>
    <TD align='center'><B>Weight<br>
    (Number only)</B></TD>
    <TD align='center'><B>Remarks</B></TD>
    </tr> 
    <%
    k=request.form("mno")
    k=cdbl(k)
    for i=1 to k 
    %>
    <tr> 
    <TD ALIGN=CENTER><B><%=i%></TD> 
    <TD><input type ='text' style="width:250px" name='mat'size=50 onblur="checkmat()"></TD>
    <TD><input type ='text' style="width:110px" name='csno'size=10 value='N/A' onblur="checkcsno()"></TD>
    <TD><input type ='text' style="width:110px" name='dsno'size=14 value='N/A' onblur="checkdsno()"></TD>
    <TD><input type ='text' style="width:60px" name='qty'size=5 value=1 onblur="checkqty()"></TD>
    <TD><input type ='text' style="width:60px" name='wt'size=5 value=0></TD>
    <TD><input type ='text' style="width:225px" name='rem'size=50 value='N/A' onblur="checkrem()"></TD>
    </tr>
    <% 
    next 
    %>
    </table>
    <br>
    <center><input type="submit" style="width:150px" value="Add"></center>
    </form>
    </body>
    </html>
    Last edited by jhardman; May 26 '08, 10:38 PM. Reason: fixed codes so it stayed in the right size
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    It looks like the validation is actually being done in the javascript, not the ASP. I'll move this to the javascript forum.

    Jared

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Instead of the ASP code, can you please post the html code only, the one you get in view source? Post only the HTML part then, not the JavaScript. JS is already clear.

      Comment

      • vikas251074
        New Member
        • Dec 2007
        • 198

        #4
        Originally posted by hsriat
        Instead of the ASP code, can you please post the html code only, the one you get in view source? Post only the HTML part then, not the JavaScript. JS is already clear.
        Yes Sir. Here is my HTML code without JavaScript. I am eagerly waiting for your suggestion.

        Thanks and regards,
        Vikas

        Code:
        <html>
        <title>Material Entry Page</title>
        <!--#include file="link_color.css"-->
        <!-- #include file="Hdr.asp" -->
        <form name="myform" action="smaterial.asp" METHOD="POST">
        
        <b>Gatepass Number   : <font color="blue"><%=GN%></font> <input type="hidden" name="gno" value="<%=GN%>" >
        <br><br>
        <TABLE border=3 cellPadding=5 cellSpacing=5 width="80%">
          <tr>
            <TD align='center'><B>SNo.</B></TD> 
            <TD align='center'><B>Material Name</B></TD> 
            <TD align='center'><B>Company Serial Number</B></TD> 
            <TD align='center'><B>Department Serial Number</B></TD>
            <TD align='center'><B>Quantity<br>
                                 (Number only)</B></TD>
            <TD align='center'><B>Weight<br>
                                 (Number only)</B></TD>
            <TD align='center'><B>Remarks</B></TD>
          </tr> 
            <tr>    
              <TD ALIGN=CENTER><B><%=i%></TD>  
              <TD><input type ='text' style="width:250px" name='mat'size=50 onblur="checkmat()"></TD>
              <TD><input type ='text' style="width:110px" name='csno'size=10 value='N/A' onblur="checkcsno()"></TD>
              <TD><input type ='text' style="width:110px" name='dsno'size=14 value='N/A' onblur="checkdsno()"></TD>
              <TD><input type ='text' style="width:60px" name='qty'size=5 value=1 onblur="checkqty()"></TD>
              <TD><input type ='text' style="width:60px" name='wt'size=5 value=0></TD>
              <TD><input type ='text' style="width:225px" name='rem'size=50 value='N/A' onblur="checkrem()"></TD>
            </tr>
        </table>
        <br>
        <center><input type="submit" style="width:150px" value="Add"></center>
        </form>
        </body>
        </html>

        Here line no. 21 to 29 is under loop using ASP code which has been removed.
        If 1 material is taken out then loop will run 1 times. Here validation works.
        If 2 material is taken out then loop will run 2 dtimes. Validation not works for > 1.
        Line no. 23, 24, 25, 26 and 28 is using validation function which was built using JavaScript.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Ok, the problem is, when you refer an element as
          var str=document.my form.mat.value, it just assumes the first (or may be last, not sure) element in the form with this name.
          So, it works only when there's one entry.
          When more then one entries, it doesn't work.

          For this, make reference as[code=javascript]var str = document.getEle mentsByName('ma t');
          for (var i=0; i<str.length; i++) {
          do_validation_f or(str[i]);
          }[/code]Hope you understand this.. :)

          And instead of the function check, check1, check2, you can reduce that code into one line by using regExp.

          Comment

          • vikas251074
            New Member
            • Dec 2007
            • 198

            #6
            I have written following function as per your suggestion. And this function is called in input tag of material name i.e. 23rd line of HTML code (without ASP code) posted here just above. But still it is not working. What wrong am I doing?

            Thanks and regards,
            Vikas

            Code:
            function checkmat()
            {
              var t;
              var str = new RegExp("\\w","i")
              str = document.getElementsByName('mat');
              if (str.length > 0)
              {
                for (var i=0; i<str.length; i++) 
                {
                  if (RegExp.test(str[i]))
                  {
            	    continue;
            	  }
            	  else
            	  {
            	    t=2;
            	    break;
            	  }
                }
                if(t==2)
                {
                  document.myform.mat.select()
                  alert("Special characters are not allowed.");
                }
              else
              {
                document.myform.mat.select()
                alert("Material must be entered.");
              }
            }

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Oh, so you are calling this function onblur event?

              Need to do a little modification then.[code=javascript]function checkmat(mat) {
              if (mat.value=="") {
              alert("Material must be entered");
              mat.focus();
              return;
              }
              else if (!mat.value.tes t(/^[\w\s]*$/i)) {
              alert("Special characters are not allowed.");
              mat.focus();
              return;
              }
              return;
              }[/code]

              And write checkmat(this) instead of checkmat().

              Comment

              • vikas251074
                New Member
                • Dec 2007
                • 198

                #8
                Very very nice sir,
                It is working now.
                Now the validation message is displaying 'Material must be entered.' It is very good. Now the problem in this function is -
                1) Else part is not working.
                2) But it got into repeated message when clicks on second material row without entering first material. And it never comes out.

                I have to use Ctrl+Alt+C to close the browser. Is there any idea to avoid this? Can I avoid user to go to second row without entering first row?

                Thanks and regards,
                Vikas

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by vikas251074
                  But it got into repeated message when clicks on second material row without entering first material. And it never comes out.
                  It shouldn't. There's some other problem in the code.

                  Originally posted by vikas251074
                  Is there any idea to avoid this? Can I avoid user to go to second row without entering first row?
                  That can be done, but you don't really need that. There's some other problem in the code as I said.

                  Also, make all the validation functions in the code similar to the material validation one.

                  Comment

                  • vikas251074
                    New Member
                    • Dec 2007
                    • 198

                    #10
                    OK Sir,

                    But the problem lies in this part, because when user clicks on second material row which is blank and the first being blank, then it gives the message for both, When I clicks on OK for 'Material must be entered' then it gives message for second row, when I clicks on OK for 'Material must be entered' then it gives message for first row. This process repeats for forever. And now Ctlr+Alt+C to close the browser.

                    What should I do?

                    Thanks and regards,
                    Vikas

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Originally posted by vikas251074
                      OK Sir,

                      But the problem lies in this part, because when user clicks on second material row which is blank and the first being blank, then it gives the message for both, When I clicks on OK for 'Material must be entered' then it gives message for second row, when I clicks on OK for 'Material must be entered' then it gives message for first row. This process repeats for forever. And now Ctlr+Alt+C to close the browser.

                      What should I do?

                      Thanks and regards,
                      Vikas
                      Ok, you must be using IE?... Is it? .. very bad for a web developer!

                      Replace mat.focus(); with var x = setTimeout(mat. focus(), 10);

                      Comment

                      • vikas251074
                        New Member
                        • Dec 2007
                        • 198

                        #12
                        Here else part is not working. Actually I have very little idea about using else if (!mat.value.tes t(/^[\w\s]*$/i)) {

                        So please tell me what is wrong with this.

                        Thanks and regards,
                        Vikas


                        Originally posted by hsriat
                        Oh, so you are calling this function onblur event?

                        Need to do a little modification then.[code=javascript]function checkmat(mat) {
                        if (mat.value=="") {
                        alert("Material must be entered");
                        mat.focus();
                        return;
                        }
                        else if (!mat.value.tes t(/^[\w\s]*$/i)) {
                        alert("Special characters are not allowed.");
                        mat.focus();
                        return;
                        }
                        return;
                        }[/code]

                        And write checkmat(this) instead of checkmat().

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          When you say it's not working, be more specific. How is it not working? What happens and what do you expect to happen?

                          Comment

                          • vikas251074
                            New Member
                            • Dec 2007
                            • 198

                            #14
                            Originally posted by acoder
                            When you say it's not working, be more specific. How is it not working? What happens and what do you expect to happen?

                            When I press just enter key in mat field or press mouse button first at mat field and then at next field it gives message 'Material must be entered.' This is fine.

                            But when I press special characters like +, -, *, $ etc in mat field and then press enter key, it does not give any message and moves to next field. This is not fine sir.


                            Thanks and regards,
                            Vikas

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Use match in place of test.

                              Comment

                              Working...