/    Sign up×
Community /Pin to ProfileBookmark

Simple javascript help

okay i, trying to validate a form… but the javascript is doing absolutely NOTHING!

[code=html]
function checkform()
{
if (document.register.username.value == ”) {
alert (“Please enter your desired username!”);
return false;
}
if (document.register.firstname.value == ”) {
alert (“Please enter your first name!”);
return false;
}
if (document.register.password.value == ”) {
alert (“Please enter your desired password!”);
return false;
}
if (document.register.email.value == ”) {
alert (“Please enter your email address!”);
return false;
}

return checkmail();
if (function checkmail(false)){
alert (“Invalid Email address!”);
return false;
}

}

function checkmail()
{
var x = document.register.email.value;
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(x)) return true;
else return false;
}
[/code]

that is my validator.js
and in my html file i have this in the head:

[code=html]<script language=”JavaScript” src=”validator.js” type=”text/javascript”></script>[/code]

my form name is register and the onsubmit is “return checkform();”

also could you help me getting the email validator do work, i wasnt sure how to make an if statement that if a function returned false then somthing happens….

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 01.2006 — Without seeing your whole page it's easier to just give you a working version:&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;Register&lt;/title&gt;

&lt;script type="text/javascript"&gt;

String.prototype.isEmailAddress = function () {return /^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/.test(this)}

function check (f) {
var e, i = 0
while (e = f.elements[i++]) {
if (!/S/.test (e.value)) {
alert (document.getElementById ? 'Please, field "' + e.previousSibling.data + '"is required' : 'Please, all fields are required.')
e.focus()
return false
}
}
}

&lt;/script&gt;

&lt;style type="text/css"&gt;
form { margin:auto; width:20em}
fieldset {padding:1ex}
label {display:block; margin:0; text-align:right}
input {margin-left:1em; width:10em}
button {display:block; margin:auto}
&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;Register&lt;/legend&gt;
&lt;label&gt;User name&lt;input name="userName"&gt;&lt;/label&gt;
&lt;label&gt;First name&lt;input name="firstName"&gt;&lt;/label&gt;
&lt;label&gt;Password&lt;input name="passWord" type="password"&gt;&lt;/label&gt;
&lt;label&gt;E-mail address&lt;input name="email" onchange="if (!this.value.isEmailAddress()) {alert ('Please, that does not appear to be a valid e-mail address.'); this.value = ''; this.focus()}"&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;
You'll note that I've added a more robust description of a valid e-mail address.
Copy linkTweet thisAlerts:
@KorAug 01.2006 — Instead of
<i>
</i>return checkmail();
if (function checkmail(false)){
alert ("Invalid Email address!");
return false;
}

try
<i>
</i>var bool = checkmail();
if (!bool){
alert ("Invalid Email address!");
return false;
}
Copy linkTweet thisAlerts:
@dknight3authorAug 01.2006 — thanks kor, but for some reason none of my javascript is working...

ohhhhhh, does the whole script not work if 1 thing is wrong??


YES! thank you kor! it works! after about 6 hours of trying to get it to work..lol
Copy linkTweet thisAlerts:
@felgallAug 01.2006 — Do you have Javascript enabled in the browser? If it isn't enabled then it wont run at all (that's why you always need to validate again on the server after forms are submitted).
Copy linkTweet thisAlerts:
@CharlesAug 01.2006 — ohhhhhh, does the whole script not work if 1 thing is wrong??[/QUOTE]Yes.
Copy linkTweet thisAlerts:
@dknight3authorAug 01.2006 — I dont like that... php is better... lol

i just want to use java cause its instant and they dont have to refresh to whole page to see if they have everything right..

i think im going to have to validate it twice though because people that dont have java enabled will be able to submit anything still... like a sql injection ?
Copy linkTweet thisAlerts:
@CharlesAug 01.2006 — Yes, always validate it twice. People with JavaScript shouldn't have to wait for validation and those without shouldn't be left out to dry. Do take a look at my example and if all else fails post your URL.
×

Success!

Help @dknight3 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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