/    Sign up×
Community /Pin to ProfileBookmark

Calling External JS from Form

Hi,

I have a user form that calls a javascript validation function. When I place the javascript in the page header it works perfectly, however when I move the script into an external .js file it stops working.

Can anyone see why its not calling the script correctly?

The form calls the script using:
onsubmit=”javascript:return validateInput(this);”

Header calls the script using:

<script language=”JavaScript” type=”text/javascript” src=”validate_user.js”></script>

The validate_user.js contains the following code:

function validateInput() {
// validate username
if (!document.createuser.username.value) {
alert(‘Please enter a username’); return false;
} else if (!document.createuser.fullname.value) {
alert(‘Please enter users full name’); return false;
} else if (!/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(document.createuser.email.value)){
alert(‘Invalid E-mail Address! Please re-enter.’); return false;
} else if (!document.createuser.company.value) {
alert(‘Please enter a company’); return false;
}
var invalid = ” “; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.createuser.password.value;
var pw2 = document.createuser.password2.value;
// check for a value in both fields.
if (pw1 == ” || pw2 == ”) {
alert(‘Please enter your password twice.’);
return false;
}
// check for minimum length
if (document.createuser.password.value.length < minLength) {
alert(‘Your password must be at least ‘ + minLength + ‘ characters long. Try again.’);
return false;
}
// check for spaces
if (document.createuser.password.value.indexOf(invalid) > -1) {
alert(‘Sorry, spaces are not allowed.’);
return false;
}
else {
if (pw1 != pw2) {
alert (‘You did not enter the same new password twice. Please re-enter your password.’);
return false;
}
else {
return true;
}
}
}

Thanks

Ben

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 12.2008 — validate_user.js needs to be in the same directory on the server as the HTML file that uses it.
Copy linkTweet thisAlerts:
@bjblackmoreauthorDec 12.2008 — Hi,

Thanks for the reply.

Both files are in the same directory, which is the root of my site.

Ben
Copy linkTweet thisAlerts:
@toicontienDec 12.2008 — Is validate_user.js spelled exactly the same as the actual file name? Capitalization can make a difference. You also don't need the "javascript:" part of the onsubmit attribute. The browser knows the value of an onsubmit attribute is JavaScript code. The "javascript:" part is actually a protocol identifier used in the href attribute that tells the browser to treat the URL as JavaScript code.

Are you getting a JavaScript error? If so, what is the error?
Copy linkTweet thisAlerts:
@bjblackmoreauthorDec 12.2008 — Ah, its OK, I've sorted it!

The original page is a php page, so I was using backslash to comment out the single quote marks around the JS alerts, but I forgot to remove these when I copied it across to the external JS file. i.e.

alert('Please enter a username'); return false;

should have been

alert('Please enter a username'); return false;

Sorry, dummy error on my part!

Thanks for your help.

Ben
Copy linkTweet thisAlerts:
@toicontienDec 12.2008 — No problem. ? Can't say I haven't done that myself.
×

Success!

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