FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


javascript function not being seen





ammonkc
I've got a simple javascript function that does validation for a small registration form. the function works fine if I put it in the <head> tag of my php page. but if I put the function in a *.js file it doesn't work. I can't figure out why it will work one way and not the other. I've got a lot of functions that I have to use so I really need to put them in a separate *.js file.
my code is below.

validate.js :

Code:

// JavaScript Document
function regval () {
var strAlert = '';
   if (document.regForm.userFname.value.length==0 ||
      document.regForm.userLname.value.length==0 ||
      document.regForm.userDept.value.length==0 ||
      document.regForm.username.value.length==0 ||
      document.regForm.password1.value.length==0 ||
      document.regForm.password2.value.length==0) {//there was a problem so build the alert msg
      if (document.regForm.userFname.value.length==0) {strAlert += "Please enter your first name" + '\n';}
      if (document.regForm.userLname.value.length==0) {strAlert += "Please enter your Last name" + '\n';}
      if (document.regForm.userDept.value=="none") {strAlert += "Please select your department" + '\n';}
      if (document.regForm.username.value.length==0) {strAlert += "Please enter your email as your username" + '\n';}
      if (document.regForm.password1.value.length==0 || document.regForm.password2.value.length==0) {strAlert += "Please complete both password fields" + '\n';}
      if (document.regForm.password1.value != document.regForm.password2.value) {strAlert += "The passwords that you entered do not match." + '\n';}
      //display the alert
      alert(strAlert);
      return false;
   }//end main if catch
   
}//end regValidate () function
   


my reg form :

Code:
<form method="post" action="register_app.php" name="regForm" onSubmit="return regval();">
        <fieldset>
         <label for="userFname">*First Name: </label>
         <input type="text" id="userFname" name="userFname"><br>
         <label for="userLname">*Last Name: </label>
         <input type="text" id="userLname" name="userLname"><br>
         <label for="userDept">*Department: </label>
         <input type="text" id="userDept" name="userDept"><br>
         <label for="userExt">Extension: </label>
         <input type="text" id="userExt" name="userExt"><br>
         <label for="username">*Email (username): </label>
         <input type="text" id="username" name="username">@panda-group.com<br>
         <label for="password1">*Password: </label>
         <input type="password" id="password1" name="password1"><br>
         <label for="password2">*Retype Password: </label>
         <input type="password" id="password2" name="password2"><br /><br />
         <input type="submit" id="submit" name="submit" value="Register">&nbsp;&nbsp;&nbsp;
         <input type="reset" value="Reset">
       </fieldset>
      </form>
Nyizsa
Did you reference to the .js file in the head section? Sometnig like <link .....>
ammonkc
Yes I did link the *.js file in the head tag. I actually have other validation functions in that same file that do work. thats why I can't figure out why this one wont at all. and I know that the function block itself is ok because it works if I place it in the head tag.
Aredon
The problem is most-probably that the JS file isn't where it needs to be.
Make sure the file validate.js DOESN'T contain any SCRIPT tags within it.
Then on your HTML page, you'd use the tag:
Code:

<script type="text/javascript" src="validate.js"></script>


e.g. if the HTML page's URL was http://www.aplusjavascript.co/themes/one.htm
and you used to above SCRIPT tag, then the JS file would need to be located here:
http://www.aplusjavascript.co/themes/validate.js

If you added a forward slash
Code:

<script type="text/javascript" src="/validate.js"></script>

It would be searching the root folder-directory.
http://www.aplusjavascript.co/validate.js

Make
Zork538
Could you post all (a zipped version) of al your code. so we can check all of it.
If you want you can remove al your specific code. Which is by the way a good way of identifing the problem. Try to remove as much code as you can, but leaving the problem in tact. That way you can spot the problem much easier.
ammonkc
sure I can zip up the files and post it. how do I post a zip file?
Aredon
This forum's software doesn't support file attachments. Maybe one of the phpBB modications allows you to do it, if not the software is open source and the feature COULD be added by manually editing the PHP files but most-probably won't. Why not just upload it to a server and link us to the ZIP file?
ammonkc
I actually just got it working. I don't even know what I did that made it work this time. but today it just started working. as far as I can tell, the code is the same as before. I just kept fooling around with it and switching things around, and it started working. i could still upload the zip files if you guys still want to take a look at it.
Related topics
*OFFICIAL* Which Browser do you use?
A use for Ajax?
Castro: U.S. hasn't responded to Katrina offer
Java script
AJAX tutorial [2nd part now updated]
Javascript function not working!!??
JavaScript Help
Expanding Javascript+CSS List
Using Javascript to Encrypt Data then POST to PHP
Off-Screen Objects Javascript
If You Are Replacing Notepad
Javascript Function Reference
Principal, Athletic Director Could Face Jail Time For Prayer
JAVASCRIPT FUNCTION- ONCLICK HELP!!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.