Naming Form Fields

Question: How can I validate a dynamically generated dropdown list? The name of the list is totally different at run time from that of the code, well I managed to get the name but am not able to retrieve the value from it. I used eval also for that, but the name contains"-" sign and its not working in the eval function. Can you please suggest me any solution?

Answer: Name attributes in HTML may contain letters, numbers, and underscores. Square brackets are allowed on the end when the fields are to be treated as an array on the server. No other characters are permitted. Putting a hyphen in a name attribute means that your HTML is invalid. Before you can get JavaScript to work properly you need valid HTML for it to interact with. So the first step is to fix the HTML so that only valid characters appear in the field name.

Next, there is never any need to use eval in JavaScript and it is extremely inefficient when you do use it - as well as creating a potential security hole.

Finally, the only fields in a form where it makes sense to use the name to reference it from JavaScript are radio buttons. All of your form fields will need to have an id in order to attach their labels in a semantically correct manner and so it makes the code much simpler if you use those ids to reference the form fields from JavaScript and leave the names for the server side script to use. That avoids the situations where the name may contain a value that the server can process but which is invalid in JavaScript.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow