/    Sign up×
Community /Pin to ProfileBookmark

form checking, sort of working

I would like the form not to submit if I have errors. My script seems to work and I get the messages but the form still submits after the fact. Here is the code in its simplist form. Its probably something easy but it is driving me nuts.

<html>

<script language=”JavaScript”>
function IsBlank(value) {
for (var x = value.length – 1; x >= 0; x–) {
if ((value.charAt(x) != ” “) && (value.charAt(x) != “”)) {
return false;
}
}
return true;
}

function validateForm(form) {
if ((IsBlank(form.userid.value)) ||
(IsBlank(form.password.value))) {
alert(“Enter ID and Password”);
}
}
</script>

<form action=”/cgi-bin/signin” method=”post” onsubmit=”validateForm(this)”;>
Customer ID:<input type=”text” name=”userid” size=”10″>
Password:<input type=”password” name=”password”size=”10″></td>
<input type=”submit” value=”Sign In”>
</form>

</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@CharlesMay 26.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;
&lt;title&gt;Sign In&lt;/title&gt;

&lt;script type="text/javascript"&gt;
&lt;!--
function validateForm (f) {
var e, i = 0;
while (e = f.elements[i++]) {if (!/S/.test (e.value)) {alert ('Field "' + e.previousSibling.data + '" is required.'); e.focus(); return false}}
}
// --&gt;
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;!--
fieldset {width:15em}
label {display:block; margin:1ex; text-align:right}
input {margin-left:1ex}
button {display:block; margin:1ex auto}
--&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="/cgi-bin/signin" method="post" onsubmit="return validateForm(this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Sign In&lt;/legend&gt;
&lt;label&gt;Customer ID&lt;input type="text" name="userid" size="10"&gt;&lt;/label&gt;
&lt;label&gt;Password&lt;input type="password" name="password"size="10"&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Sign In&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @ronhextall 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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