/    Sign up×
Community /Pin to ProfileBookmark

Can anyone tell me the code for looping through a form and checking if any of the fields are empty and, if they are, putting up an alert box saying ‘You have not completed all the fields’ and putting the focus on the first control with no data in it.
Thanks for any help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 05.2003 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<style type="text/css">

<!--

label {display:block; margin:1em 0em}

input {display:block}

-->

</style>

<script type="text/javascript">

<!--

function validate(f) {

var i;

for (i=0; i<f.elements.length; i++) {if (/^s*$/.test(f.elements[i].value)) {

alert ('You have not completed all the fields.');

f.elements[i].focus();

return false;

}}

}

// -->

</script>

<form action="" onsubmit="return validate(this)">

<div>

<label>Fee<input type="text"></label>

<label>Fie<input type="text"></label>

<label>Foe<input type="text"></label>

<label>Fum<input type="text"></label>

<button type="submit">Submit</button>

</div>

</form>[/font]
Copy linkTweet thisAlerts:
@WebskaterauthorAug 05.2003 — Thanks very much.
×

Success!

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