View Full Version : "Building" field names?

07-11-2005, 01:44 PM
Please can anybody tell me how to do this:

I'm working with a shopping cart and for every item selected from a catalogue, the user must enter the quantity.
The problem I have is, due to the fact that the quantity field changes its name for every product selected, how can I
validate the content of the field in Javascript (e.g. >0, only numbers, etc)?.
I've tried to "build" the name of the fields in JS scripts, but it doesn't work.
I'm attaching an example of my JS:

<script language="JavaScript">
// num is the name of the field in html form

function verify_qty(num)
var name_field='document.form_cart'+ num;

var regex = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
if (!regex.test(name_field.value)) {
alert('You can only enter numbers here.');



07-11-2005, 01:55 PM
Use square bracket notation. (See my sig for more info)

var name_field = document.nameOfTheForm.elements["form_cart" + num];