Call Javascript from a different file

Hello I want to call the following file from a different file:

<script language = "Javascript">
function validateForm(){

      var VCity=document.Form1.City
      var VState=document.Form1.State

      if ( VCity.value  == "") {
            VCity.value = ""
          alert( "Please fill in a city" )
              VCity.focus()
          return false;
      }
      
      
      if ( VState.value == "") {
            VState.value = ""
          alert( "Please fill in a state" )
              VState.focus()
          return false;
      }
      
    return true
}
 </script>



It validates a form.  I tried to name it validate.js and call it with the following:

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

Do you know why this happens or how to fix it?  Thanks!
smoothcat11Asked:
Who is Participating?
 
basicinstinctCommented:
take the script tags out of validate.js
ie, the contents of that file should just be:


function validateForm(){

      var VCity=document.Form1.City
      var VState=document.Form1.State

      if ( VCity.value  == "") {
            VCity.value = ""
          alert( "Please fill in a city" )
              VCity.focus()
          return false;
      }
     
     
      if ( VState.value == "") {
            VState.value = ""
          alert( "Please fill in a state" )
              VState.focus()
          return false;
      }
     
    return true
}
0
 
Jai STech ArchCommented:
you should not use text...jst use this alone
<script language="JavaScript" src="graphics/validate.js"></script>

and make sure that your file is in the specified folder
0
 
basicinstinctCommented:
@jaiganeshsrinivasan

actually it is the 'language' attribute that has been deprecated, the 'type' attribute is the w3c standard way to do it, but there is no harm having both... if either of them were to go it should be 'language'
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Pratima PharandeCommented:
<script language="JavaScript" type="text/javascript" src="graphics/validate.js"></script>

make sure path is right

For above code if your calling file must be in the root directory and
js file like below

and root/graphics/validate.js

Can you share your diectory structure with us ?

what error you are getting ?
0
 
Jai STech ArchCommented:
i think Business Intelligence(sorry - Basic Instinct) is right...the file should not contain the script tag...becos it is in the script tag that we are specifying the content....apologies...
0
 
siva_sivaCommented:
Use semicolon in all the line..

function validateForm(){
      var VCity=document.Form1.City;
      var VState=document.Form1.State;

      if ( VCity.value  == "") {
            VCity.value = "";
          alert( "Please fill in a city" );
              VCity.focus();
          return false;
      }
           
      if ( VState.value == "") {
            VState.value = "";
          alert( "Please fill in a state" );
              VState.focus();
          return false;
      }
     
    return true;
}
0
 
Michel PlungjanIT ExpertCommented:
siva_siva: As with the type attribute - not important.
Semicolons are only mandatory in event handlers like
onClick="var x=2
alert(x)"

which will not work unless you add ; after x=2

Also two statements on one line needs semicolon between them:

function alertTwo() {
  alert(a); alert(b)
}
0
 
Michel PlungjanIT ExpertCommented:
@smoothcat11 : once you have fixed the file, consider passing the form to the function using the "this" keyword
Also why set the field to blank when it is blank???:

<form onSubmit="return validate(this)">

with the script looking like this

function validateForm(theForm) {
  var VCity=theForm.City;
  if ( VCity.value  == "") {
    alert( "Please fill in a city" )
    VCity.focus()
    return false;
  }
  var VState=theForm.State;
  if ( VState.value == "") {
    alert( "Please fill in a state" )
    VState.focus()
    return false;
  }
  return true
}


Michel.

PS: Points to BasicInstincts if you still had script tags in the external file
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.