473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

validation of mandatory fields

19 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. function validateBatchNumber(){
  2.         <% j = 1 %>
  3.         for(var i=0;i<document.form1.BatchFields.length;i++)
  4.         { alert(document.form1.BatchFields[i].value)
  5.             <%  
  6.               sql = "select * from batchcontrol where fieldID ='"& j &"' order by fieldID"
  7.               Rs.open sql,oConn 
  8.               if trim(Rs("isMandatory")) = "1" then %>
  9.                 if (document.form1.BatchFields[i].value = "")
  10.                 { 
  11.                     return false;
  12.                 }
  13.  
  14.               <% end if  
  15.               j = j + 1 %>
  16.  
  17.         }
  18.         return true; 
  19. }
  20.  
can anyone please help.

warm regards
Jun 26 '08 #1
7 3162
acoder
16,027 Recognized Expert Moderator MVP
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.
Jun 26 '08 #2
sva0008
19 New Member
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?
Jun 27 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
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.
Expand|Select|Wrap|Line Numbers
  1. // arr is ASP-generated array containing names of mandatory fields
  2. for (i = 0; i < arr.length; i++) {
  3.     field = document.forms["form1"].elements[arr[i]];
  4.     // now validate here...
  5.  
Jun 27 '08 #4
Claus Mygind
571 Contributor
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


Expand|Select|Wrap|Line Numbers
  1. <HEAD>
  2.  
  3. <SCRIPT LANGUAGE="JavaScript">
  4. <!--
  5.   // batch validation router on form submit
  6.    function validateForm(form) {
  7.  
  8.          var aErrorList = new Array();
  9.          var chkList = ""; 
  10.  
  11.          if (isNotEmpty(form.LastName)) {
  12.              aErrorList[aErrorList.length] = "Last Name"
  13.          }
  14.  
  15.          if (isNotEmpty(form.FirstName)) {
  16.              aErrorList[aErrorList.length] = "First Name"
  17.          }
  18.  
  19.          if (isNotEmpty(form.MrMrs)) {
  20.              aErrorList[aErrorList.length] = "Mr/Mrs"
  21.          }
  22.  
  23.          if (aErrorList.length == 0){
  24.              return true;
  25.          }else{
  26.              for (var i = (aErrorList.length-1); i > -1 ; i--) {
  27.                   if (i == 0){   
  28.                       chkList += aErrorList[i]
  29.                   }else{                           
  30.                       chkList += aErrorList[i]+","+"\n"
  31.                   }                                
  32.              }
  33.              alert("Check the following: "+"\n"+chkList);
  34.                return false;
  35.          }
  36. ////end validate before submittal//////////
  37.  
  38. // validates that the field value string has one or more characters in it
  39. function isNotEmpty(elem) {
  40.    var str = elem.value;
  41.    var re  = /.+/;
  42.    if(!str.match(re) || str == " ") {           
  43.      setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
  44.      return true;
  45.    } else {        
  46.      return false;
  47.    }
  48. }
  49. //-->
  50. </SCRIPT>
  51. </HEAD>
  52.  
  53. <BODY >
  54.  
  55. <FORM 
  56.     METHOD=POST 
  57.      ACTION="" 
  58.      NAME="f1"
  59.      onSubmit="return false"
  60.      >
  61.  
  62.      <INPUT 
  63.          TYPE="button" 
  64.            onClick="if (validateForm(this.form)) {SaveSubmit(this.form)};"
  65.            name="MySubmit" 
  66.            id="MySubmit" 
  67.            value="Add" 
  68.       />
  69.  
  70.  
  71.    <INPUT 
  72.         TYPE="text" 
  73.         NAME="MrMrs"
  74.      id  ="MrMrs"
  75.       />
  76.    <INPUT 
  77.         TYPE="text" 
  78.         NAME="FirstName"
  79.      id  ="FirstName"
  80.       />
  81.    <INPUT 
  82.         TYPE="text"
  83.         NAME="LastName"
  84.      id  ="LastName"
  85.       />
  86.  
  87. </FORM>
  88. </BODY>
  89.  
Jun 27 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
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:
Expand|Select|Wrap|Line Numbers
  1. <form 
  2.     method="POST"
  3.      action="" 
  4.      name="f1"
  5.      onsubmit="return validateForm(this.form)">
  6.  
Jun 28 '08 #6
sva0008
19 New Member
I am using this code but it dosent work.

Expand|Select|Wrap|Line Numbers
  1. function validateBatchNumber(){
  2.         <% for j =1 to 5 
  3.  
  4.            sql = "select * from batchcontrol where fieldID ='"& j &"' order by fieldID"
  5.             Rs.open sql,oConn 
  6.  
  7.         //for(var i=0;i<document.form1.BatchFields.length;i++)
  8.         //{ alert(document.form1.BatchFields[i].value)
  9.  
  10.              if trim(Rs("isMandatory")) = "1" then %>
  11.                 var i = <%=j-1%>
  12.                 alert(document.form1.BatchFields[i].value)
  13.                 if (document.form1.BatchFields[i].value = "")
  14.                 {   
  15.                     alert("please enter required field")
  16.                     return false;
  17.                 }
  18.  
  19.              <% end if 
  20.              rs.close 
  21.              next  %>
  22. }

can anyone tell me whats wrong in this code
Jul 1 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
On line 13, you're setting instead of comparing. It should be:
Expand|Select|Wrap|Line Numbers
  1. document.form1.BatchFields[i].value == ""
Please use code tags when posting code.
Jul 1 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2732
by: Ollie | last post by:
I have defined a set of SOAP messages using an xsd file, each element in the xsd has the minoccurs, maxoccurs & nillable attributes defined when I then use xsd.exe to generation C# classes for...
16
2423
by: printline | last post by:
I have a problem with some validation of some fields in a form. I have some fields that only become visible if a specific field is chosen. For example: <select...
4
4019
by: kiwipedia | last post by:
Hi everyone - here is my problem: I've got an Access 2002 database with a table and a field (not the primary key) called . This table has a one to many relationship with a table called . ...
4
2087
by: plumba | last post by:
Let me explain.... I have a form (most of which you guys have helped me with!!). The initial problem I was having is that it was holding the data in the fields after submit, I resolved this by...
6
1713
by: Dameon99 | last post by:
Ive been looking through a whole tonne of sites to try to find out how I can make my fields mandatory but havnt been able to find something so far which actually tells me whwta I want to know. Ive...
1
1207
by: TheB1GDude | last post by:
OK so its been a while since I have had to put some field checking code together, I thought what I had done was fairly simple but nothing ever is, is it!? This my script code: <SCRIPT...
1
1375
by: ajd335 | last post by:
Hi.. there are some radio buttons,which are mandatory in my site. So how can i find whether they are filled by the user or not ..so that i can respond accordingly.(i.e if all mandatory fields are...
1
1703
pradeepjain
by: pradeepjain | last post by:
Hii , I have a form which has 20-30 textboxes which are mandatory ! is there a way of validating mandatory fields in easier ways by using a single function or some thing like tht ! bcos i...
0
986
by: pragati ganga | last post by:
I am using wizard,i did validation using javascript but the problem is dat wenever the alert message will appear after checking the validation if i click OK button dat it is going to next page...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.