Unable to call function from after adding js file

Javascript coding ..

Moderators: macek, egami, gesf

Gamers
New php-forum User
New php-forum User
Posts: 48
Joined: Mon Jun 11, 2012 9:13 am

Unable to call function from after adding js file

Postby Gamers » Tue Aug 20, 2013 7:31 am

Hi,

I am new to this technology, I had created rules.js file in which i mentioned validate function which is used for required field validator for textarea. but after adding the js file in my html page i unable to call the function please let me know what i am doing wrong.

rules.js file

Code: Select all

<script>
   
   function validate()
   {
      var req=document.forms["registration"]["comments"].value;
      
      if (req==null || req=="")
      {
         alert("");
         return false;
      }
   }
</script>


html page

Code: Select all

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


Form tag

Code: Select all

<form id="registration" name="registration" method="POST" action="" onsubmit="return validate();">

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: Unable to call function from after adding js file

Postby sandeep605085 » Tue Aug 20, 2013 11:40 am

Try below if that helps you.

<html>
<head>
<script type='text/javascript'>
function validate()
{
var req=document.forms["registration"]["comments"].value;

if (req==null || req=="")
{
alert("no value");
return false;
} else {
alert(req);
return false;
}
}
</script>
</head>
<body>
<form id="registration" name="registration" method="POST" action="" onsubmit="return validate();">
<textarea name='comments'></textarea>
<input type='submit' value='submit' name='submit'>
</form>
</body>
</html>


If you are writing the javascript code in a js file and including in html like below
<script type="text/javascript" src="rules.js"></script>

Then check that whether your js file loaded in the browser or not.
from firefox you can install firebug addon and can check under js tab in firebug that rule.js is loaded or not.

Thanks,
Sandeep Agrawal
Mindfire Solutions

Gamers
New php-forum User
New php-forum User
Posts: 48
Joined: Mon Jun 11, 2012 9:13 am

Re: Unable to call function from after adding js file

Postby Gamers » Tue Aug 20, 2013 11:07 pm

Thanks sandeep for the reply, yes its work when i write the function on the same html page but i dont want like this i want the function to be call from the javascript file.

I checked that the java script file is loaded on my html page i can see it from view source


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest