javascript beginner
I am getting the following error but cannot see what is wrong with code. I have done similar in previous workshops and they are ok? I have the "required" function in workshop12.js and want to use it to check that input is not empty. I took code from w3schools http://www.w3schools.com/js/js_validation.asp
enter image description here
js
$( document ).ready(function() {
// Check that input is not empty
function required() {
var x = document.forms["loginForm"]["inputName"].value;
document.write (x);
if(x = " ") {
alert("Please input a value");
return false;
}
else {
alert("Correct");
return true;
}
}
});
html
<body>
<div class="container">
<div class="row">
<div class="col-sm-offset-2 col-sm-10 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6">
<h2>Login form</h2>
</div>
<div class="clearfix visible-md clearfix visible-lg"></div>
</div>
<form name="loginForm" class="form-horizontal">
<div class="form-group">
<label for="inputName" class="col-sm-2 col-md-2 col-lg-3 control-label">Name</label>
<div class="col-sm-10 col-md-8 col-lg-6">
<input type="text" class="form-control" name="inputName" "id="inputName" placeholder="Name" onclick="required()">
<div class="col-md-2 col-lg-3"></div>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 col-md-2 col-lg-3 control-label">Email</label>
<div class="col-sm-10 col-md-8 col-lg-6">
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
<div class="col-md-2 col-lg-3"></div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10 col-md-offset-3 col-md-9 col-lg-offset-3 col-lg-9">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
<script type="text/javascript" src="js/workshop12.js"></script>
</body>
-
Looks like you're also using jQuery. Did you remember to include that as well?j08691– j086912015年03月09日 17:29:36 +00:00Commented Mar 9, 2015 at 17:29
-
2you are hiding it inside the scope of the ready function.Daniel A. White– Daniel A. White2015年03月09日 17:30:20 +00:00Commented Mar 9, 2015 at 17:30
-
@j08691 yes have included jquery- <script src="ajax.googleapis.com/ajax/libs/jquery/1.11.1/…>Pamela Keogh– Pamela Keogh2015年03月09日 17:36:20 +00:00Commented Mar 9, 2015 at 17:36
-
if(x == " ") //use double equalsAlaksandar Jesus Gene– Alaksandar Jesus Gene2015年03月09日 17:40:48 +00:00Commented Mar 9, 2015 at 17:40
-
Oops I honestly did have double equalsPamela Keogh– Pamela Keogh2015年03月09日 17:45:26 +00:00Commented Mar 9, 2015 at 17:45
2 Answers 2
Your function is in-scope only within the ready function, it is not available outside that scope.
Your element is looking for that function in global scope. Move the function outside the ready function as demonstrated below:
$( document ).ready(function() {
... whatever
});
// Check that input is not empty
function required() {
var x = document.forms["loginForm"]["inputName"].value;
document.write (x);
if(x = " ") {
alert("Please input a value");
return false;
}
else {
alert("Correct");
return true;
}
}
5 Comments
You have a scoping problem.
There are different kind of scopes within Javascript.
Global Scope: a variable / function that is scoped global is accessible in each function. Normally you don't want to scope anything global, because in times where everyone uses different frameworks, you never know who writes a variable into the global scope.
Example for a global scope:
window.bla = "I am global scoped";
var globalVar = "I am also global scoped";
function accessGlobal(){
console.log (globalVar)
}
Now there's also a function scope: variables declared within a function are locally scoped and are not accessible from outside (If you are more like the Java guys, think of it as a private variable)
function private(){
var privateVar = "I am a private var";
}
Your required function is NOT globally accessible due to the fact that you used jQuery's onReady function.
See this:
$( document ).ready(function() {
You see the function? Because of this, your required function is function scoped.
In your case, I would recommend the Module Pattern to prevent polluting the global scope.
Try this:
var myModule = {};
myModule.required = function(){
var x = document.forms["loginForm"]["inputName"].value;
document.write (x);
if(x.length === 0) {
alert("Please input a value");
return false;
}
else {
alert("Correct");
return true;
}
}
and in your HTML code, use replace required() with myModule.required();
By the way, in your required function, you made another mistake. You tried to check wether a variable is empty like this:
if (x = " ")
In Javascript you need to check variables using == or better ===. (I've changed this in your code)
The difference between == and === is simply that === also checks for the correct type.
E.g.
"1" == 1 //true
"1" === 1//false because string and number are different