By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,527 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,527 IT Pros & Developers. It's quick & easy.

Form should submit only when all data is filled

vikas251074
100+
P: 198
I now creating material outpass for taking material out from our campus.
In the first page, persons name taking material out, issue_date, department, vehicle_no, driver_name, purpose, received_by and remarks is entered.
In the second page no. of materials to taken out is given.
In the third page, it creates no. of row as mentioned in the second page. suppose 5. Five row is created. Fields in this page are material_name, company_sr_no, department_sr_no, quantity, weight, remarks. Two buttons are there. One is 'Cancel' and other is form submit button 'Add'.

Here in third page, one problem regular occurs. When enter key is pressed by mistake without filling all details, the form is submitted (i.e. 'Add' button is pressed). How can I stop this? I want to there should be no effect of enter key without filling all the five row. If any one tries to click 'Add', 'Add' button should not work untill and unless all the five rows of material are filled.

Thanks and regards,
Vikas
Jun 2 '08 #1
Share this Question
Share on Google+
11 Replies


gits
Expert Mod 5K+
P: 5,390
hi ...

add an onsubmit-handler to your form and call a javascript function that validates all the fields:

Expand|Select|Wrap|Line Numbers
  1. <form action="what_ever" onsubmit="return validate_form(this);">
the function should check all fields and has to return either true or false depending on your validation:

Expand|Select|Wrap|Line Numbers
  1. function validate_form(node) {
  2.     var val = true;
  3.  
  4.     // validation code sets the val that should be returned
  5.     // note the parameter node is a reference to your form already
  6.  
  7.     return val;
  8. }
  9.  
kind regards
Jun 2 '08 #2

vikas251074
100+
P: 198
hi ...

add an onsubmit-handler to your form and call a javascript function that validates all the fields:

Expand|Select|Wrap|Line Numbers
  1. <form action="what_ever" onsubmit="return validate_form(this);">
the function should check all fields and has to return either true or false depending on your validation:

Expand|Select|Wrap|Line Numbers
  1. function validate_form(node) {
  2.     var val = true;
  3.  
  4.     // validation code sets the val that should be returned
  5.     // note the parameter node is a reference to your form already
  6.  
  7.     return val;
  8. }
  9.  
kind regards

Each record contains five fields, material_name, comp_sr_no, dept_sr_no, qtyl, wt.
And suppose there are three records. Now I want to check only material_name, if all the three records are filled with material_name. How can I check this? I don't know how to do this. I have tried to searched this solution on internet.

Thanks and regards,
Vikas
Jun 2 '08 #3

gits
Expert Mod 5K+
P: 5,390
please post an example of your form since it would be a kind of guesswork without seeing what you have done so far ...

kind regards
Jun 2 '08 #4

vikas251074
100+
P: 198
please post an example of your form since it would be a kind of guesswork without seeing what you have done so far ...

kind regards

OK Sir,

Should I provide code? If yes, then following is code.
Attached Files
File Type: txt mat.txt (4.7 KB, 289 views)
Jun 2 '08 #5

gits
Expert Mod 5K+
P: 5,390
hmmm ...

try the following:

Expand|Select|Wrap|Line Numbers
  1. function validate_form(node) {
  2.     var val = true;
  3.  
  4.     // get the list of all 'mat'-fields
  5.     var list = node.getElementsByName('mat');
  6.  
  7.     for (var i = 0, n; n = list[i]; i++) {
  8.         // in case any value of a 'mat' field is empty we
  9.         // return false
  10.  
  11.         if (n.value == '') {
  12.             val = false;
  13.             break;
  14.         }
  15.     }
  16.  
  17.     return val;
  18. }
  19.  
kind regards
Jun 2 '08 #6

vikas251074
100+
P: 198
I have added the above code as follows

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function validate_form(node) 
  3. {
  4.   var val = true;
  5.   // get the list of all 'mat'-fields
  6.   var list = node.getElementsByName('mat');
  7.   for (var i = 0; n = list[i]; i++) 
  8.   {
  9.     // in case any value of a 'mat' field is empty we return false
  10.     if (n.value == '') 
  11.     {
  12.       val = false;
  13.       break;
  14.     }
  15.   }
  16.   return val;
  17. }
  18. </script>

and modified the following line.

Expand|Select|Wrap|Line Numbers
  1. <form name="myform" action="smaterial.asp" method="post" onSubmit="return validate_form(this)">
Note:- action="smaterial.asp" is calling another file where message is displayed for submission of form i.e. 'Record has been saved.'

On running programme, form is submitted even without entering any value. Then I changed some line as follows -

Expand|Select|Wrap|Line Numbers
  1. function validate_form(node) 
  2. {
  3.   var val = true;
  4.   var n; 
  5.   // get the list of all 'mat'-fields
  6.   var list = node.getElementsByName('mat');
  7.   for (var i = 0; i < 3; i++) 
  8.   {
  9.     // in case any value of a 'mat' field is empty we
  10.     // return false
  11. alert("Form Submit Status");
  12.     n = list[i]
  13.     if (n.value == '') 
  14.     {
  15.       val = false;
  16.       break;
  17.     }
  18.   }
  19.   return val;
  20. }
But this is also not working. I think function validate_form(this) is not calling the function because alert message is also not displaying. How could I complete this job.

Thanks and regards,
Vikas
Jun 3 '08 #7

gits
Expert Mod 5K+
P: 5,390
sorry my bad ... replace the node-reference with a document reference:

Expand|Select|Wrap|Line Numbers
  1. function validate_form() {
  2.     var val = true;
  3.  
  4.     // get the list of all 'mat'-fields
  5.     var list = document.getElementsByName('mat');
  6.  
  7.     for (var i = 0, n; n = list[i]; i++) {
  8.         // in case any value of a 'mat' field is empty we
  9.         // return false
  10.  
  11.         if (n.value == '') {
  12.             val = false;
  13.             break;
  14.         }
  15.     }
  16.  
  17.     return val;
  18. }
  19.  
kind regards

btw: don't use the language-attribute in script tags since it is deprecated ... use type:

[HTML]<script type="text/javascript">[/HTML]
and write onsubmit all in lowercase ...
Jun 3 '08 #8

P: 30
I now creating material outpass for taking material out from our campus.
In the first page, persons name taking material out, issue_date, department, vehicle_no, driver_name, purpose, received_by and remarks is entered.
In the second page no. of materials to taken out is given.
In the third page, it creates no. of row as mentioned in the second page. suppose 5. Five row is created. Fields in this page are material_name, company_sr_no, department_sr_no, quantity, weight, remarks. Two buttons are there. One is 'Cancel' and other is form submit button 'Add'.

Here in third page, one problem regular occurs. When enter key is pressed by mistake without filling all details, the form is submitted (i.e. 'Add' button is pressed). How can I stop this? I want to there should be no effect of enter key without filling all the five row. If any one tries to click 'Add', 'Add' button should not work untill and unless all the five rows of material are filled.

Thanks and regards,
Vikas

use javascript functions to cary out validation..

call this function when the form is submited by using the event handler
onSubmit
Jun 3 '08 #9

gits
Expert Mod 5K+
P: 5,390
use javascript functions to cary out validation..

call this function when the form is submited by using the event handler
onSubmit
as i said already: it is 'onsubmit' instead of 'onSubmit' ... it is a node attribute and all node-attributes should be written in lowercase and their values should be enclosed in double-quotes ... read post #8 -> this should work ...

kind regards
Jun 3 '08 #10

vikas251074
100+
P: 198
Yes sir,

This problem has been solved now. This is working fine.

Thanks and regards,
Vikas
Jun 4 '08 #11

gits
Expert Mod 5K+
P: 5,390
no problem ... post back to the forum anytime you have more questions ...

kind regards
Jun 4 '08 #12

Post your reply

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