/    Sign up×
Community /Pin to ProfileBookmark

from validation, a little more complex than usual

Ok, I have a form generated from asp with x amount of fields. I’d like to validate all of those fields to make sure they only contain integers. Simple to do loop on all form elements that are of type text… however there’s also another section of the same form with y fields that should contain text. That’s where I am hung up.
How can I validate them before submitting the form?

I can have all field names that need to be validated in an array if that helps.
var fields=new Array(“IntegerField1”, “IntegerField2”, “IntegerField3”,…);

Thanks ahead of time for your help, I really do need it and appreciate it.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rigadonAug 18.2005 — So you only want to validate the x fields and not the y fields? Can you not name them differently so you can distinguish them? Try this:

[CODE]function do_validate(frm)
{ var form = document.getElementById(frm);
for (i=0; i<form.elements.length; i++)
{ if (form.elements[i].type == 'text' &&
form.elements[i].name.match(/^x_/))
{ alert('gud: ' + i + ' - ' + form.elements[i].name + ' - ' + form.elements[i].type + ' - ' + form.elements[i].value);
} else
{ alert('bad: ' + i + ' - ' + form.elements[i].name + ' - ' + form.elements[i].type + ' - ' + form.elements[i].value);
}
}
}[/CODE]


This script looks for all 'text' fields whose names begin 'x_'...
Copy linkTweet thisAlerts:
@nzakasAug 18.2005 — If you have the IDs of all fields that need to be validated in an array, just do this:

<i>
</i>for (var i=0; i &lt; fields.length; i++) {
var field = document.getElementById(fields[i]);
validate(field.value);
}
×

Success!

Help @wardo 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...