validation of mandatory fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sva0008
    New Member
    • Apr 2008
    • 19

    validation of mandatory fields

    Hi ,
    I have a scenario in which i have four input fields in asp with the same name batchlist.
    I have a button called add batch .
    The user can add the batch by clicking the add batch button . out of the four asp input field there are some field which the user cannot leave blank . we can know that mandatory field by the SQL table by checking the Ismandatory field.

    i want to validate it by writing a javascript so that i make sure user enter the field which are mandatory.

    I have written the following code but it dosent work.


    [CODE=javascript]function validateBatchNu mber(){
    <% j = 1 %>
    for(var i=0;i<document. form1.BatchFiel ds.length;i++)
    { alert(document. form1.BatchFiel ds[i].value)
    <%
    sql = "select * from batchcontrol where fieldID ='"& j &"' order by fieldID"
    Rs.open sql,oConn
    if trim(Rs("isMand atory")) = "1" then %>
    if (document.form1 .BatchFields[i].value = "")
    {
    return false;
    }

    <% end if
    j = j + 1 %>

    }
    return true;
    }
    [/CODE]

    can anyone please help.

    warm regards
    Last edited by gits; Jun 26 '08, 05:55 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You're mixing ASP and JavaScript. Once the page has loaded, ASP won't execute. To see what I mean, look at the source code of the loaded page in your browser.

    Comment

    • sva0008
      New Member
      • Apr 2008
      • 19

      #3
      Originally posted by acoder
      You're mixing ASP and JavaScript. Once the page has loaded, ASP won't execute. To see what I mean, look at the source code of the loaded page in your browser.

      i know but can anyone please let me know how to do this?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You could generate an array using ASP which has the mandatory fields, print them out to the screen (within script tags) to make them available to JavaScript, then loop over them using the form elements array, e.g.
        [code=javascript]// arr is ASP-generated array containing names of mandatory fields
        for (i = 0; i < arr.length; i++) {
        field = document.forms["form1"].elements[arr[i]];
        // now validate here...
        [/code]

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          When streaming out your html page from the server you could add the following to your fields to validate
          1. In the header add your batch validation and a function to test if field is empty
          2. Turn off form submission in the form tag so form is not submitted when the user presses the enter key
          3. Add a button to form which will submit form if form is valid.
          4. Adding the array to the form validation routine will allow you to show any and all missed fields.

          Besides doing this it is also good to add field validation to the form instead of having the user wait until the form is submitted.

          Since these can be overcome by hackers, it is still recommended that you add server side validation of the submitted form


          Code:
          <HEAD>
          
          <SCRIPT LANGUAGE="JavaScript">
          <!--
            // batch validation router on form submit
             function validateForm(form) {
           
                   var aErrorList = new Array();
                   var chkList = ""; 
          
                   if (isNotEmpty(form.LastName)) {
                       aErrorList[aErrorList.length] = "Last Name"
                   }
          
                   if (isNotEmpty(form.FirstName)) {
                       aErrorList[aErrorList.length] = "First Name"
                   }
          
                   if (isNotEmpty(form.MrMrs)) {
                       aErrorList[aErrorList.length] = "Mr/Mrs"
                   }
           
                   if (aErrorList.length == 0){
                       return true;
                   }else{
                       for (var i = (aErrorList.length-1); i > -1 ; i--) {
                            if (i == 0){   
                                chkList += aErrorList[i]
                            }else{                           
                                chkList += aErrorList[i]+","+"\n"
                            }                                
                       }
                       alert("Check the following: "+"\n"+chkList);
               		  return false;
                   }
          ////end validate before submittal//////////
          
          // validates that the field value string has one or more characters in it
          function isNotEmpty(elem) {
             var str = elem.value;
             var re  = /.+/;
             if(!str.match(re) || str == " ") {           
               setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
               return true;
             } else {        
               return false;
             }
          }
          //-->
          </SCRIPT>
          </HEAD>
          
          <BODY >
          
          <FORM 
              METHOD=POST 
          	 ACTION="" 
          	 NAME="f1"
          	 onSubmit="return false"
          	 >
          
               <INPUT 
                   TYPE="button" 
                	 onClick="if (validateForm(this.form)) {SaveSubmit(this.form)};"
                	 name="MySubmit" 
                	 id="MySubmit" 
                	 value="Add" 
                />
          
          
             <INPUT 
          	    TYPE="text" 
          	    NAME="MrMrs"
               id  ="MrMrs"
          	  />
             <INPUT 
          	    TYPE="text" 
          	    NAME="FirstName"
               id  ="FirstName"
          	  />
             <INPUT 
          	    TYPE="text"
          	    NAME="LastName"
               id  ="LastName"
          	  />
          
          </FORM>
          </BODY>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Claus Mygind
            2. Turn off form submission in the form tag so form is not submitted when the user presses the enter key
            3. Add a button to form which will submit form if form is valid.
            You don't need to return false from onsubmit to prevent the enter key submitting the form. You can check for the key pressed. If it's the enter key and it's not in a textarea element, you can ignore it. Call the validation function onsubmit:
            Code:
            <form 
                method="POST"
            	 action="" 
            	 name="f1"
            	 onsubmit="return validateForm(this.form)">

            Comment

            • sva0008
              New Member
              • Apr 2008
              • 19

              #7
              I am using this code but it dosent work.

              Code:
              function validateBatchNumber(){
                      <% for j =1 to 5 
                      
                         sql = "select * from batchcontrol where fieldID ='"& j &"' order by fieldID"
                          Rs.open sql,oConn 
                      
              		//for(var i=0;i<document.form1.BatchFields.length;i++)
              		//{ alert(document.form1.BatchFields[i].value)
              		     
              		     if trim(Rs("isMandatory")) = "1" then %>
              		        var i = <%=j-1%>
              		        alert(document.form1.BatchFields[i].value)
              		        if (document.form1.BatchFields[i].value = "")
              		        {   
              		            alert("please enter required field")
              		            return false;
              		        }
              		        
              		     <% end if 
              		     rs.close 
              		     next  %>
              }

              can anyone tell me whats wrong in this code
              Last edited by acoder; Jul 1 '08, 08:42 PM. Reason: Added [code] tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                On line 13, you're setting instead of comparing. It should be:
                [code=javascript]document.form1. BatchFields[i].value == ""[/code]

                Please use code tags when posting code.

                Comment

                Working...