0

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>
asked Mar 9, 2015 at 17:27
5
  • Looks like you're also using jQuery. Did you remember to include that as well? Commented Mar 9, 2015 at 17:29
  • 2
    you are hiding it inside the scope of the ready function. Commented Mar 9, 2015 at 17:30
  • @j08691 yes have included jquery- <script src="ajax.googleapis.com/ajax/libs/jquery/1.11.1/…> Commented Mar 9, 2015 at 17:36
  • if(x == " ") //use double equals Commented Mar 9, 2015 at 17:40
  • Oops I honestly did have double equals Commented Mar 9, 2015 at 17:45

2 Answers 2

4

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; 
 }
}
answered Mar 9, 2015 at 17:30
Sign up to request clarification or add additional context in comments.

5 Comments

I'd also note that there might be something already on the global scope named required. In chrome when you call an undefined it logs required() VM244:2 Uncaught ReferenceError: required is not defined and in firefox required() ReferenceError: required is not defined
@ Daniel A White & Jamiec. I have moved it as recommended but still getting same error
@Austin. Cheers. I changed the name and that solved it.
Thanks everyone for your help.Very much appreciated
There's an error! if (x = " ") will never work. See my answer
2

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
answered Mar 9, 2015 at 17:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.