What is wrong with my javascript that is preventing alert and/or form validation?

I am trying to just build a simple form validation function so that I can build me google chrome extension. Later on I will be finishing up a twitter API call, but need to be able to post the values that are entered in to the input fields I have.

Break Down

  • Chrome will only use a html, css, and javascript
  • Form will use the value from the text field and the number field
  • The onsubmit I have added to the form should be posting the data in the value fields over to the javascript and having it processed
  • There is one ifelse statement that I have that is checking the value of "hashtag" to make sure it is not blank
  • Finally you will notice some document.write() that I am using to test to see where in the javascript it is not reading... nothing is working

I feel like I have gone over this a millions times. Any help would be appreciated thank you.

!DOCTYPE html>
<html>
<head>
<!-- Start javascript -->
<script type="text/javascript">
function validator(){
 //   variables that pull the input elements from my form
    var hashtag = document.getElementById("hashtag").value;
    var tweetamy = document.getElementById("tweetamt").value;
    document.write(hashtag);
    // validating input fails if left blank if false shows alert popup, if ture write the contents of the value (this is for testing)
   if(x == "") {
    // Shows a popup when the search bar is empty
      alert("Error: Input is empty!");
      return false;
    } else {
        //prints what ever was in the hashtag element in the form
        document.write(hashtag);
        return true;
    }
}
</script>
<!-- End javascript -->
</head>
<body>
<h3> Search hashtags on twitter (ex: #winning)</h3>
<br>
<!-- The start of my form, has an "onsubmit" action-->
<form method="post" onsubmit="javascript:validator();">
<!-- Used as a search bar -->
  Search:<input type="text" name="search" id="hashtag" />
  <br><br>
  <!-- Used to set the amount of tweets in the last hour you want to pull -->
  Tweets:<input type="number" name="key_amt" min="0" max="100" value="1"id="tweetamt" />
  <br><br>
  <!-- Just a button -->
     <input type="submit" value="Search" />
</form>
<!-- end of form -->
</body>
</html>

Answers


You need to move all your JavaScript code to an external file.

Inline JavaScript will not be executed. This restriction bans both inline script blocks and inline event handlers.

The first restriction wipes out a huge class of cross-site scripting attacks by making it impossible for you to accidentally execute script provided by a malicious third-party. It does, however, require you to write your code with a clean separation between content and behavior.

https://developer.chrome.com/extensions/contentSecurityPolicy#JSExecution


in

if(x == "") {

'x' is not defined, I think you want:

if(hashtag == "") {

Need Your Help

How do I change the background color of the column with the expanders in Eclipse?

eclipse ide editor themes

Recently after scorching my retinas from the garish white background in Eclipse, I found some of the excellent posts about how to change the colors so it uses sane (i.e. dark background) colors in ...

Erf() in VBA-Excel code doesn't work in Excel 2003

excel-vba add-in vba excel

I need to calculate the error function, Erf(), in my VBA Excel program. I have implemented something like that: