View Full Version : Dynamic Form and Field Names
putts
02-23-2004, 08:39 AM
I have a form-validation script I'm building where I'm going to send in the form's name to the script and then it is going to get a list of that form's fields from a hidden field placed in the form.
However, I'm not sure on how to do the whole "dynamic form name" stuff in javascript.
I think it should go something like...
<script language="javascript">
function validateForm(formName)
{
var strFldList = document.forms(formName).elements("fieldList").value;
var arrFlds = strFldList.split(",")
var blnValid = true;
for(i=0;i<=arrFlds.length();i++)
{
if(document.forms(formName).elements(arrFlds(i)).value = "")
blnValid = false;
}
if (blnValid)
document.forms(formName).submit();
else
window.alert("Please complete the Form and re-submit!");
}
</script>
<body>
<form name="myForm" id="myForm">
<input type="hidden" name="fieldList" id="fieldList" value="Sample1,Test2" />
Sample Field 1: <input type="text" name="Sample1" id="Sample1" />
<br />
Test Field 2: <input type="text" name="Test2" id="Test2" />
<br />
<a href="javascript: validateForm('myForm');">Submit Form</a>
</form>
</body>
Thanks for the help!
agent002
02-23-2004, 08:51 AM
JavaScript:
<script type="text/javascript"><!--
// Fields that can be empty
var dontcheck = new Array('somefield3', 'somefield4');
// Names for the fields
var fieldnames = new Array();
fieldnames['somefield1'] = 'Some field 1';
fieldnames['somefield2'] = 'Some field 2';
fieldnames['somefield3'] = 'Some field 3';
fieldnames['somefield4'] = 'Some field 4';
function validateForm(which){
var dclist = ';' + dontcheck.join(';') + ';';
var notfilled = new Array();
for(var i = 0; i < which.elements.length; i++){
if(which.elements[i].value == '' && dclist.indexOf(';'+which.elements[i].name+';') < 0){
notfilled[notfilled.length] = fieldnames[which.elements[i].name];
}
}
if(notfilled.length > 0){
alert('Please fill the following fields before submitting the form: '+notfilled.join(', '));
return false;
}
return true;
}
--></script>
HTML:
<form action="filename.cgi" method="post" onsubmit="return validateForm(this);">
Some field 1: <input type="text" name="somefield1"><br>
Some field 2: <input type="text" name="somefield2"><br>
Some field 3: <input type="text" name="somefield3"><br>
Some field 4: <input type="text" name="somefield4"><br>
<input type="submit" value="Submit">
</form>
bassrek
02-23-2004, 08:56 AM
This thread started by yours truly might help, too:
http://www.htmlforums.com/showthread.php?s=&threadid=34732
agent002
02-23-2004, 09:09 AM
Originally posted by bassrek
This thread started by yours truly might help, too:
http://www.htmlforums.com/showthread.php?s=&threadid=34732
I think the code I posted in this thread is a bit more advanced.
putts
02-23-2004, 09:13 AM
< -- See below message -- >
putts
02-23-2004, 09:20 AM
Here's the current working function with the eval statement....
<script language="javascript">
function validateForm(formName)
{
var strFldList = document.getElementById(formName).fieldList.value;
var arrFlds = strFldList.split(",");
var blnValid = true;
var testValue;
for(i=0;i<arrFlds.length;i++)
{
testValue = eval("document." + formName + "." + arrFlds[i] +".value;")
if(testValue == "")
blnValid = false;
}
if (blnValid)
document.getElementById(formName).submit();
else
window.alert("Values must be completed to submit!");
}
</script>
...and I suppose that works fine. Still thinking there should be some way to do that tho.
agent002
02-23-2004, 09:21 AM
I see you don't like my code, so I'll fix yours then :P
document.getElementById(formName).elements(arrFlds[i]).value;
should be
document.getElementById(arrFlds[i]).value;
agent002
02-23-2004, 09:25 AM
I think the way you are doing this is rather complicated. First off, you have a list of the fields to be validated - but stored in an <input> element? Why?? Then you use a for loop to check every element listed in that list. The script I provided you earlier simply checks every element in the form, and you can specify a list of elements that you don't want checked.
putts
02-23-2004, 09:27 AM
Yeah, I know which is based off the theorem that all fields should have their own unique field ID (which is the "standard" way to do it), however, I have a very weird scenario where I'm not sure of that fact as all the field names/ids will be coming from database tables and so I was hoping to be able to keep it regulated to the individual forms.
I may just have to force some "standardization" on the whole thing.
Thanks again.
bassrek
02-23-2004, 09:33 AM
Hey putts, if you're not sure all fields will be unique, would it be possible in your situation to add a pre or suffix to the field with a counter inside your ASP code? I've been faced with similar situations like that before and have done that to help.
putts
02-23-2004, 10:23 AM
Well, see, this is how it works.
To build a form that is capable of editing several fields from one table, I prefix each field name/id with the Autonumber ID from the table for that record (leaving the "new" record with no prefix) - this builds a form that has the look and feel of a table or Excel object.
This is usually fine and dandy as I know enough that if I'm gonna have two of these "table forms" on one page, I don't set up the same field name in each table.
However, I'm building a Relational Database Builder where people can create and manage the back end of their web sites (pretty spiffy, eh?) and if they have several tables set up as a "many" side to a "one" table, then they could, possibly, have two tables with the same field name sitting on the same edit page.
Now, by prefixxing the field names with the table's autonumber ID, the duplications of the those field names is very rare indeed, however, the duplication of that name could exist in the "new" record fields.
The saving grace, as far as processing goes, is that the fields would be in separate forms.
That's where the dynamic form/field names thing came into play.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.