/    Sign up×
Community /Pin to ProfileBookmark

How’s Your Javascript?

I’m trying to figure out something in javascript that is just escaping me…

In a web form, I am trying to validate some fields when the user hits the Submit button, using the tag:
form name=myform onSubmit=”return validate()”

In the validate(myform) javascript I am trying to access the values that the user entered (in field “version”):

This works: alert(“version = ” + myform.version.value);

This gives an error:
var fieldname = “version”;
alert(“version = ” + myform.fieldname.value);
+++ myform.fieldname has no properties

How can I get “fieldname” to be evaluated BEFORE it figures out the address myform.fieldname.value?
Here’s what doesn’t work:
myform.fieldname.value
myform.(fieldname).value
myform.${fieldname}.value

Any thoughts? You guys are the best minds in the field! If you don’t know, then it can’t be done!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 01.2007 — alert("version = " + myform[fieldname].value);

ofcourse the best method would still be using document.getElementsByTagName and document.getElementById
Copy linkTweet thisAlerts:
@savvykmsNov 01.2007 — I think this`ll work for validating if all the TEXT fields in a form are filled in. It should stop the form being submitted without input in all the text fields specified in the 2nd parameter ("field1,field2"). Notice how it is a string using commas to seperate form items under the form which is the 1st parameter.
[CODE]
<script type="text/javascript" language="javascript">
<!--
function validate(form,fields) {
var temp = fields.split(",");
var value = "";
for (var i = 0; i < temp.length; i++) {
value = eval(form+"."+temp[i]+".value");
if (!value) { return false; }
}
return true;
}
-->
</script>
[/CODE]

[CODE]
<form action="http://www.google.com/" method="get" onsubmit="return validate('inputform','t1,t2,t3')" name="inputform">
<input type="textbox" name="t1">
<input type="textbox" name="t2">
<input type="textbox" name="t3">
<input type="submit" value="Go">
</form>
[/CODE]
Copy linkTweet thisAlerts:
@mrhooNov 01.2007 — var fieldname = "version";

alert("version = " + myform[fieldname].value);
Copy linkTweet thisAlerts:
@toicontienNov 01.2007 — You can also use [B]myform.elements[fieldname].value[/B]. This is the recommended way of getting the value of a form element. The .elements property of a FORM tag contains references to all the form fields, buttons, and form field groups in a form as an integer-indexed array and an associative array.

JavaScript, DOM, and the Humble FORM
Copy linkTweet thisAlerts:
@PensacolaAuxauthorNov 01.2007 — Thanks to all that replied. I think we got it fixed.
×

Success!

Help @PensacolaAux 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.22,
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,
)...