• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Required fields from a dynamic number of form fields

I am using PHP to program a page that loops through to see how many items I have set on the backend. It performs a while loop to show the inputted number of items set on the backend. It starts like this:
 <? $cnt = $row->numberofitems + 1;
      $i = 1;
      while($i < $cnt)
      {
      $queryb = "SELECT * FROM table;

So I might have 3 form fields added or whatever number is set. My question is how can I set these up to be required. I am using javascript to check required fields and the code is shown below:

function MM_validateForm(){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
if (document.contactfrm.fname.value == ''){
errors+='-First name is required.\n';
}
if (document.contactfrm.lname.value == ''){
errors+='-Last name is required.\n';
}
if (document.contactfrm.email.value == ''){
errors+='-Email address is required.\n';
}
if (errors)
alert ('The following errors occured:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->

</script>      
0
nisupport
Asked:
nisupport
  • 2
  • 2
  • 2
1 Solution
 
raj3060Commented:
function MM_validateForm(){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
var returncode = false;
if (document.contactfrm.fname.value == ''){
returncode = true;
errors+='-First name is required.\n';
}
if (document.contactfrm.lname.value == ''){
returncode = true;
errors+='-Last name is required.\n';
}
if (document.contactfrm.email.value == ''){
errors+='-Email address is required.\n';
returncode = true;
}
if (returncode){
alert ('The following errors occured:\n'+errors);
return false;
}
}
//-->

</script>  
0
 
DigitalTyrantCommented:
if you are calling the function from the <Form> tag's onsubmit event, you must declare "return MM_validateForm();" and make sure that the function returns a boolean value.  If false, the submission of the form will be cancelled.
0
 
nisupportAuthor Commented:
I do have the return MM_validateForm() in the form tag. I tried to add the returncode and it ended up submitting a blank form.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
DigitalTyrantCommented:
have you checked that MM_validateForm() returns a true or false value based on the validation?

if everything is valid, return true, else return false.
0
 
raj3060Commented:
Did you try my code?

it should be called:

<form onsubmit='return MM_validateForm()'>
0
 
nisupportAuthor Commented:
I did add the form onsubmit and it still submitted without any values. I am wondering if it is becuase some of the fields don't exist in the form. For example there is only bksess1, bksess2, bksess3. But other times it might go up to bksess5 or there could be none.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now