/    Sign up×
Community /Pin to ProfileBookmark

Validating HTML form with array field type

Hi,

I want to validate an HTML form, that have array filed names. For example

[code=php]
<INPUT TYPE=”TEXT” NAME=”contact[name]”>
<INPUT TYPE=”TEXT” NAME=”contact[email]”>
[/code]

I need this as a program require contact us form in this format (sunshop).

I tried,

[code=php]
if(document.formname.contact[name].value.length==0) { alert(‘You must enter name’); return false; }
[/code]

But not working.

You can see sample form at

[url]http://yujinboby.bizhat.com/javascript_validate_form_array.html[/url]

Can anyone tell me how to validate the form ?

Regards,

Yujin

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@CharlesDec 03.2005 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;

<i> </i> &lt;script type="text/javascript"&gt;

function check (f) {
var required = ['contact[name]', 'contact[email]']
var e, i = 0
while (e = f.elements[required[i++]]) {
if (e.type == 'text' &amp;&amp; !/S/.test (e.value)) {
alert ('Please, field "' + e.parentNode.firstChild.data + '" is required.')
e.focus()
return false
}
}
}

<i> </i> &lt;/script&gt;

<i> </i> &lt;style type="text/css"&gt;

form {margin:auto; width:20em}
fieldset {padding:1ex}
label {display:block; text-align:right}
input {margin-left:1ex}
button {display:block; margin:auto}

<i> </i> &lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="some-script.pl" onsubmit="return check (this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Contact&lt;/legend&gt;
&lt;label&gt;Name&lt;input name="contact[name]" type="text"&gt;&lt;/label&gt;
&lt;label&gt;Email&lt;input name="contact[email]" type="text"&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @netfreehost 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...