Code Mat
Code Mat

Reputation: 21

Include JavaScript in HTML

I'm new to HTML and developing and login form. Please find below the code I used. I want to import my JavaScript code into HTML form.please help me.

C:\Program Files\xampp\htdocs\forsiteSystem\thems\js\validation.js

 function validateForm()
{
var x=document.forms["login"]["email"].value;
if (x==null || x=="")
  {
  alert("Email must be filled out");
  return false;
  }
} 

C:\Program Files\xampp\htdocs\forsiteSystem\thems\login.html

<html>
        <head>
            <title>Login form</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <script src="js/validation.js">
            </script>
        </head>
        <body>
            <div>
                <form name="login" action="" method="post" onsubmit="return validateForm()">
                    Email: <input type="text" name="email"><br>
                    password: <input type="text" name="pass"><br>
                    <input type="submit"  id="Submit_button" >
                </form>
            </div>
        </body>
    </html>

Upvotes: 0

Views: 133

Answers (5)

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

Include your js file properly like this:

<script src="js/validation.js">

This code works for me.try

<html>
        <head>
            <title>Login form</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <script src="js/builder.js">
            </script>
        </head>
        <body>
            <div>
                <form name="login" action="" method="post" onsubmit="return validateForm()">
                    Email: <input type="text" name="email"><br>
                    password: <input type="text" name="pass"><br>
                    <input type="submit"  id="Submit_button" >
                </form>
            </div>
        </body>
    </html>

Your js file will be on same directory under js file with this name builder.js. Code in js file is :

function validateForm()
{
var x=document.forms["login"]["email"].value;
if (x==null || x=="")
  {
  alert("Email must be filled out");
  return false;
  }
} 

Upvotes: 3

dev
dev

Reputation: 439

this is how you can add js correctly

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

Upvotes: 0

Mahendra Jella
Mahendra Jella

Reputation: 5596

Js path shoulb be js/validation.js

<html>
    <head>
        <title>Login form</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="js/validation.js" type="text/javascript">
        </script>
    </head>
    <body>
        <div>
            <form action=".\login.php" method="post" onsubmit="return validateForm()">
                Email: <input type="text" name="email"><br>
                password: <input type="text" name="pass"><br>
                <input type="submit"  id="Submit_button" >
            </form>
        </div>
    </body>
</html>

Upvotes: 0

msapkal
msapkal

Reputation: 8346

Script path should be.

<script src="js\validation.js"></script>

Upvotes: 0

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

This tag should work:

<script src="js/validation.js"></script>

Upvotes: 1

Related Questions