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>
5 Answers 5
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;
}
}
answered Jan 10, 2014 at 10:35
Mahmood Rehman
4,3377 gold badges43 silver badges76 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Code Mat
im verry sorry to say still it doesn't work for me .
Mahmood Rehman
what is your file hierarchy ?
Code Mat
C:\Program Files\xampp\htdocs\forsiteSystem\thems\js\validation.js C:\Program Files\xampp\htdocs\forsiteSystem\thems\login.html
Mahmood Rehman
Did you use my
js or your own ? and how you include jsCode Mat
your js. i have included login.html <script src="js/validation.js"> </script>
|
This tag should work:
<script src="js/validation.js"></script>
answered Jan 10, 2014 at 10:35
DontVoteMeDown
21.5k10 gold badges72 silver badges113 bronze badges
Comments
Script path should be.
<script src="js\validation.js"></script>
answered Jan 10, 2014 at 10:35
msapkal
8,3462 gold badges34 silver badges48 bronze badges
1 Comment
dev
please check the path you used as src
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>
answered Jan 10, 2014 at 10:40
Mahendra Jella
5,5962 gold badges35 silver badges38 bronze badges
Comments
this is how you can add js correctly
<script type="text/javascript" src="js/validation.js"></script>
Comments
default
login.html, you're not closing the meta tag, is that normal?