/    Sign up×
Community /Pin to ProfileBookmark

Form Validation is being a ruhtard.

Index:

[code]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-type” content=”text/html; charset=utf-8″ />
<meta http-equiv=”Content-language” content=”en” />
<title>Random Number Genorator</title>
<script type=”text/javascript” src=”rand-js.js”></script>
<link rel=”stylesheet” type=”text/css” href=”rand-css.css” />
</head>
<body>
<div id=”wrapper”>
<form id=”numbers” action=”javascript: generate()” onsubmit=”javascript: return checkValid()”>
<fieldset>
<legend>Number Range</legend>
1st Number: <input type=”text” name=”num1″ id=”num1″ size=”4″ maxlength=”4″ tabindex=”1″ /><br />
2nd Number: <input type=”text” name=”num2″ id=”num2″ size=”4″ maxlength=”4″ tabindex=”2″ /><br />
<input type=”submit” value=”Generate” />
</fieldset>
</form>
</div>
</body>
</html>[/code]

JS File:

[code]function generate()
{
window.alert(‘Oh, hai. Script and **** be working.’);
}
function checkValid()
{
var errors = new Array(‘Please fill in all fields.’,’Both entries must be ONLY numbers.’,’Please use only WHOLE numbers.’);
var err_flag = false;

var form = document.numbers;

if(form.num1 == null || form.num1 == ” || form.num2 == null || form.num2 == ”)
{
err_flag = true;
window.alert(errors[0]);
return false;
}
}[/code]

I really cannot find anything wrong with it. What I’m trying to do is check if the fields are empty and then stopping the script if they are; however, it doesn’t! Chome user here.

Please help… I’m a really decent webguy, but I was just reminded why I stick to the server side.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 08.2010 — if(form.num1[COLOR="Blue"].value[/COLOR] == null || form.num1[COLOR="Blue"].value[/COLOR] == '' || form.num2[COLOR="Blue"].value[/COLOR] == null || form.num2[COLOR="Blue"].value[/COLOR] == '')
Copy linkTweet thisAlerts:
@Ashes226authorApr 08.2010 — OH!!! Oh my god! Thank you!

You see, PHP isn't very OO, ya know?
Copy linkTweet thisAlerts:
@Ashes226authorApr 08.2010 — Oh... It still is not working.
Copy linkTweet thisAlerts:
@FangApr 08.2010 — document.numbers references a [I]name [/I]value, not the [I]id[/I]

The pseudo protocol [I]javascript:[/I] is generally unnecessary.
&lt;form [COLOR="Blue"]name[/COLOR]="numbers" action="" onsubmit="return checkValid()"&gt;
×

Success!

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