/    Sign up×
Community /Pin to ProfileBookmark

Javascript dynamic field validation

I’ve a page which renders a group of input fields x times depending on the user’s choice of x from a dropdown (1-6).
I want to validate their input (mandatory fields) so I do this using Javascript with a function IsEmpty to which I pass the field name and text to display in an alert.
I can’t concatenate a string with the field name (e.g. lastname_ + ’01’) as I receive an error that object doesn’t exist.
Can someone please explain how I can perform validation on the dynamic fields?

Thanks in advance,


to post a comment

3 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 28.2008 — We need to see the code, so far.
Copy linkTweet thisAlerts:
@nac20authorJul 28.2008 — Please see a static version of the code below which works, simplified to show one input field called txtFirstName.

My dynamic page containing the form 'frmStep1' will contain 1-6 instances, i.e. txtFirstName_0 - txtFirstName_5 depending on user input from a previous page

so I want to be able to create a loop in the Javascript to check the input accordingly, I'm guessing I therefore need to pass in the number as a parameter to checkInput.

I tried lots of combinations to get this working and kept hitting errors stating that the object didn't exist.

<form ... name="frmStep1" onSubmit="return checkInput();">


function isEmpty(formElement, message) {

formElement.value = trim(formElement.value);

_isEmpty = false;
if (formElement.value == '') {
_isEmpty = true;

return _isEmpty;


function checkInput()


with (window.document.frmStep1) {

if (isEmpty(txtFirstName, 'Enter first name')) {

return false;

} else {

return true;



Copy linkTweet thisAlerts:
@KorJul 28.2008 — <i>
</i>&lt;form name="frmStep1" onsubmit="return checkInput(this);"&gt;

</i>&lt;script type="text/javascript"&gt;
function isEmpty(formElement, message){
formElement.value = trim(formElement.value);
var _isEmpty = false;
if (formElement.value == ''){
_isEmpty = true;
return _isEmpty;
function checkInput(f){
var i=0, t;
if(isEmpty(t,'Please fill all the required fields!')){return false}


Help @nac20 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 9.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @ddiebold17,
tipped: article
amount: 1000 SATS,

tipper: @Mqlinka19,
tipped: live stream
amount: 4900 SATS,

tipper: @Mqlinka19,
tipped: article
amount: 10 SATS,