/    Sign up×
Community /Pin to ProfileBookmark

How to read "[" as a variable

This is a crazy question but I have been going nuts trying to solve my dilemna. I have a form that posts to submit.php. Before the form posts I would like to check the fields for blanks and stop the form from submitting using a simple javascript code that works great. Here is my problem!

The field names are info[name] and are not recognized by the javascript. The submit.php needs the names to be this way and I was hoping for a work around.

Here is what a field looks like:

<INPUT name=info[Phone] size=”20″ value=”<? echo $phone; ?>”>

I tried changing the name to be inside quotes but it still doesn’t work.

Here is a portion of the code that isn’t working:

function frmLifeStyle_Validator(theForm)
{

if (theForm.info[Fname].value == “”)
{
alert(“Please enter a value for the “First Name” field.”);
theForm.info[Fname].focus();
return (false);
}

if (theForm.info[Fname].value.length > 50)
{
alert(“Please enter at most 50 characters in the “First Name” field.”);
theForm.info[Fname].focus();
return (false);
}

Is there anyway to get the names of the fields to work this way?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 21.2008 — theForm.elements['info[Fname]'].value
Copy linkTweet thisAlerts:
@felgallMay 21.2008 — The alternative is to not reference the fields by name from the JavaScript and reference them by id instead.
Copy linkTweet thisAlerts:
@ChazzlMay 21.2008 — Yep! DOM is the way to go. Assign an ID to your Input element and address it via DOM [I]getElementById('')[/I] in your javascript:

[code=html]<html>
<head>
<title>How to read "[" as a variable</title>
<script type="text/javascript">
function frmLifeStyle_Validator(theForm){
var objFname = document.getElementById('FnameID');
if (objFname.value == ""){
alert("Please enter a value for the "First Name" field.");
objFname.focus();
return (false);
}
if (objFname.value.length > 50){
alert("Please enter at most 50 characters in the "First Name" field.");
objFname.focus();
return (false);
}
}
</script>
</head>
<body>
<INPUT name="info['Fname']" id="FnameID" size="20" value="<? echo $Fname; ?>">
</body>
</html>
[/code]


The reason why it doesn't work in the original instance is because [B]info[Fname][/B] is read by Javascript as an array when it doesn't exist as an array. Thus, unable to operate. ID attributes ftw.
Copy linkTweet thisAlerts:
@FangMay 21.2008 — 
The reason why it doesn't work in the original instance is because [B]info[Fname][/B] is read by Javascript as an array when it doesn't exist as an array. Thus, unable to operate. ID attributes ftw.[/QUOTE]
It was incorrectly referenced.

Why add an [I]id[/I] when it's not necessary?
Copy linkTweet thisAlerts:
@Declan1991May 21.2008 — DOM does not only mean document.getElementById. It also means document.all and document.layers, although those DOMs are now out-of-use.

One might as well use document.getElementsByName("")[0] rather than cluttering up the HTML with unnecessary ids.
Copy linkTweet thisAlerts:
@ChazzlMay 21.2008 — Interesting. Although, [i]document.getElementsByName("")[0][/i] doesn't describe the element well to the programmer. Get a large application and that could be excruciating to track down each reference and cost more to maintain.

I prefer ID attributes over Name Attributes when referencing elements in the HTML Hierarchy. It separates data handling from interface management, which is necessary occasionally.

It is the balance of efficiency with maintainability, which we all measure differently.
×

Success!

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