PDA

View Full Version : Errors with form with multiple fields named the same


DiaH
08-20-2002, 04:36 PM
I have a form that has 4 fields on it that is like a datasheet, they repeat for each record in the form. In order to use them in an array for a different function, they are all named the same for each record (ie: F1, F2, F3, F4 are repeated for every record).

How can I validate what the user entered with all the fields appearing multiple times on a form?

I also need need to make a checkbox checked when the field value has been changed. How can I do this?

Thanks,
Dia

brothercake
08-20-2002, 04:44 PM
if you have multiple form fields with the same name, they form an array, so

document.forms["formname"]["F1"][0]
document.forms["formname"]["F1"][1]

etc.

DiaH
08-20-2002, 04:47 PM
I don't know how many records will be on the form. The array issue is different. I just want to be able to validate, like with onchange event, what the user has entered. Since the fields for all records have the same name, if I just use the onChange function the browser doesn't know which record's field I am talking about.

I set up a function that loops through the entire form to find the field's ordinal position but then it continues to validate every instance of that field and I don't want it to do that.

brothercake
08-20-2002, 05:16 PM
if you're using onchange for validation, you can just use this

DiaH
08-20-2002, 05:18 PM
Originally posted by brothercake
if you're using onchange for validation, you can just use this

This?? this what??

Spookster
08-20-2002, 06:09 PM
LOL exactly.

this.what


sorry that was funny.

what brothertwinkie meant was something along these lines:

<select name="whatever" onChange="validationFunction(this.value);">

you would need to know the name of the field as you can just pass the value to the function using the this keyword.

DiaH
08-20-2002, 06:11 PM
Originally posted by Spookster
LOL exactly.

this.what


sorry that was funny.

what brothertwinkie meant was something along these lines:

<select name="whatever" onChange="validationFunction(this.value);">

you would need to know the name of the field as you can just pass the value to the function using the this keyword.

Yes, I realized my reply was somewhat funny, as it made me laugh too! I had never heard of "this" but is that just for js or vbs? I am not too proficient with js.

Spookster
08-20-2002, 06:26 PM
In Javascript "this" refers to the current object so you can say

this.attribute

which would refer to the current objects attribute.


I don't work with VBScript so I can't say for sure.

DiaH
08-20-2002, 06:29 PM
"This" reminded me of the "me" function in vbs. "me.value" does the same thing.

Thanks........

I'm sure I'll be back with more questions.

adios
08-20-2002, 07:54 PM
Analyze this:

1) What sort of validation did you have in mind?

2) Could you elaborate on this:
I also need need to make a checkbox checked when the field value has been changed.

adios (I love stuff like this) :cool:

DiaH
08-20-2002, 07:57 PM
Originally posted by adios
Analyze this:

1) What sort of validation did you have in mind?

2) Could you elaborate on this:


adios (I love stuff like this) :cool:

1) Validation for the fields where only Y or N is to be entered; 1 set of fields is a numerator/denominator combo so need to check if numerator value is smaller than denominator value.

2) The text boxes will be filled using a recordset. If the user makes a change to the field value, I want to flag it for update so I don't have to update all the records regardless of data change.

adios
08-20-2002, 08:34 PM
DiaH...

I think you may be too smart for me (not joking).....

Never heard of a 'numerator/denominator combo' and the rest is pretty opaque as well. You sound like you can probably handle this yourself; if you'd still like some (possible) assistance, I for one would need a really concise description of the form, its fields, their organization, etc.

cheers :confused: , adios

DiaH
08-20-2002, 08:41 PM
Originally posted by adios
DiaH...

I think you may be too smart for me (not joking).....

Never heard of a 'numerator/denominator combo' and the rest is pretty opaque as well. You sound like you can probably handle this yourself; if you'd still like some (possible) assistance, I for one would need a really concise description of the form, its fields, their organization, etc.

cheers :confused: , adios

I suppose I should explain more.

THere are 2 fields on the form, one called "numerator" and one called "denominator" that are used for exactly what they are labeled. There is a button on the form that calculates an answer by taking the numerator field divided by the denominator field (regular ol' math). I need to validate that the value in the "numerator" is less than the field in the "denominator". Make more sense now?

As far as the rest of the validation, the user has the ability to edit the values that have already been entered. When the user clicks the submit button, I don't want to have to loop through all the records on the form and update all the records when perhaps only 1-2 records got changed. I may end up just looping through them all anyway b/c this is getting to be a headache for me.

Dia

adios
08-20-2002, 10:54 PM
See what you can make of this:


<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function get_quotient(num,denom) {
if (!num.value || /^\s$/.test(num.value) || isNaN(Number(num.value))) {
alert('Please enter a valid numerator.');
num.focus();
num.select();
return false;
}
if (!denom.value || /^\s$/.test(denom.value) || isNaN(Number(denom.value))) {
alert('Please enter a valid denominator.');
denom.focus();
denom.select();
return false;
}
if (Number(num.value)>Number(denom.value)) {
alert('Denominator value must be greater than numerator value!');
denom.focus();
denom.select();
return false;
}
num.form.quotient.value = Math.round((Number(num.value)/Number(denom.value))*100)/100;
return true;
}

function checkChange(f) {
var flds = 'F1F2F3F4group', i = 0;
while (f.elements[i].name && f.elements[i].name != 'group') ++i;
while (f.elements[i].name && flds.indexOf(f.elements[i].name) != -1)
if (f.elements[i].name != 'group' && f.elements[i].value == f.elements[i].defaultValue)
f.elements[i++].disabled = true;
else i++;
return true;
}

</script>
</head>
<body bgcolor="pink">
<form onsubmit="return get_quotient(this.numerator,this.denominator)&&checkChange(this)">
<hr>
<strong>Numerator: </strong><input name="numerator" type="text" size="12">
<strong>Denominator: </strong><input name="denominator" type="text" size="12">
<input name="q" type="button" value="Quotient >>"
onclick="get_quotient(numerator,denominator)">
<input name="quotient" type="text" size="12" readonly="readonly">
<hr>
<input name="group" type="hidden" value="4">
F1 <input name="F1" type="text"><br>
F2 <input name="F2" type="text"><br>
F3 <input name="F3" type="text"><br>
F4 <input name="F4" type="text"><br><br>
<input name="group" type="hidden" value="4">
F1 <input name="F1" type="text"><br>
F2 <input name="F2" type="text"><br>
F3 <input name="F3" type="text"><br>
F4 <input name="F4" type="text"><br><br>
<input name="group" type="hidden" value="4">
F1 <input name="F1" type="text"><br>
F2 <input name="F2" type="text"><br>
F3 <input name="F3" type="text"><br>
F4 <input name="F4" type="text"><hr>
<input type="submit" value="DONE">
</form>
</body>
</html>

Just fooling around; since I can only guess how this is being handled server-side, it's only a beginning - possibly the end as well.....

DiaH
08-20-2002, 11:39 PM
Close but no cigar.

The numerator and denominator fields could as well appear multiple times on the form. Think grid / database.

When I clicked the done button at the end I got a runtime error on line 30, elements[...].name is null or not an object. I filled in all of the textboxes so every one has a value.

I'm not quite sure how you're checking for edit to the values, but the rest I think I can decifer.


Dia

adios
08-21-2002, 12:06 AM
DiaH...

Edited the above a bit; let me take a look for that error. The basic approach is this: separate the groups of (4) fields with a hidden field; any element (text) that doesn't have an altered value gets disabled - which means it isn't included in the submission. No flags necessary, if it hasn't been altered, it won't be there.

cya later, adios

p.s. are you editing as we - edit? I may need more sleep:eek:

DiaH
08-21-2002, 12:16 AM
I've attached a file to this post that is a picture of what the form can look like. In this instance, the numerator/denominator fields only show once but they can show multiple times.