139

Receiving the subject error when Chrome tries to load the script file on the page. It says it's at the last line of the javascript file. I can't seem to find anything wrong with it. No errors in firefox, and the script works as expected. Just using form validation

// JavaScript Document
$(function() {
  $('#wm-form').submit(function() {
    var errors = false;
    var errorMsg = "";
    $('.required').each(function() {
      if(!validField($(this))) {
        errorMsg += $(this).attr('name').capitalize() + " cannot be blank\n";
        errors = true;
      }
    });
    var emailAddress = $('#email');
    if(isValid(emailAddress) && !(/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/.test(emailAddress.val()))) {
      errorMsg += "Not a valid email address. Please enter in a correctly formatted email address";
      errors = true;
    }
    if(errors) {
      alert(errorMsg);
      return false;
    }
  });

  $('.form-focus').click(function() {
    $(document).scrollTop(0);
    $('#first_name').focus();
    return false;
  });
});

function validField(element) {
  if(!isValid(element.val()) || (element.attr('placeholder') && element.attr('placeholder') == element.val()) || 
    (element.attr('type') == 'radio' && !checkedRadio(element))) {
    return false;
  }
  else {
    return true;
  }
}

function isValid(ele) {
  if(ele == null || ele == '') {
    return false;
  }
  else {
    return true;
  }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

function checkedRadio (element) {
  var valid = false;
  $('input[name="'+ element.attr("name") +'"]:checked').each(function() {
    valid = true;
  });

  return valid;
}​
Community
  • 1
  • 1
agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • same problem with ajax when I'm using a php5 class who contain an extra echo for test, json returned data became invalid – khaled_webdev Dec 27 '12 at 10:14
  • I found that the problem is solved if the JSON returned file is in a **SINGLE LINE** Hope that helps – my account_ram Jul 17 '13 at 19:51
  • 1
    In the event that you are experiencing this in WordPress, enqueue the scripts from functions.php. I had a specific template where I was calling for the JS directly from the template. Switching to a conditional enqueue in either wp_head or wp_footer resolved this. – Alpesh Shah Oct 08 '14 at 20:43
  • Just by opening the problematic file in Notepad++, removing the last character (blank) and saving the file again as UTF-8 without BOM fixed the issued for me. – Marcos Buarque Jun 24 '15 at 18:12
  • Not addressed here is possible "smart" replacement of characters, such as the Mac's curly quotes “” ‘’. Turning off text replacement or searching/replacing these characters can solve this problem. – OxC0FFEE May 01 '16 at 14:23

4 Answers4

261

There's some sort of bogus character at the end of that source. Try deleting the last line and adding it back.

I can't figure out exactly what's there, yet ...

edit — I think it's a zero-width space, Unicode 200B. Seems pretty weird and I can't be sure of course that it's not a Stackoverflow artifact, but when I copy/paste that last function including the complete last line into the Chrome console, I get your error.

A notorious source of such characters are websites like jsfiddle. I'm not saying that there's anything wrong with them — it's just a side-effect of something, maybe the use of content-editable input widgets.

If you suspect you've got a case of this ailment, and you're on MacOS or Linux/Unix, the od command line tool can show you (albeit in a fairly ugly way) the numeric values in the characters of the source code file. Some IDEs and editors can show "funny" characters as well. Note that such characters aren't always a problem. It's perfectly OK (in most reasonable programming languages, anyway) for there to be embedded Unicode characters in string constants, for example. The problems start happening when the language parser encounters the characters when it doesn't expect them.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 2
    Wow, thanks for posting a solution for this. I did not believe at first that my code was not working because of copy/paste, but I thought I would give it a try and this fixed it. – Metropolis Jul 18 '11 at 15:45
  • 1
    In my case, I had TextMate to convert Tabs to Space and it all went fine in Chrome :) Weird. – StuFF mc Oct 17 '11 at 14:00
  • In my case, %0A was breaking my javascript call.. new lined. – abelito Feb 22 '12 at 13:22
  • 81
    +1 Thanks pointy. Cut and paste from jsFiddle gave me that character. – iambriansreed May 15 '12 at 16:01
  • I had same prob, I found them with Vi, do you knwo any way to find them from Eclipse? –  Aug 01 '12 at 11:04
  • me three, copy and paste from jsfiddle and I got this error – JBeckton Sep 22 '12 at 00:22
  • 4
    This is commonly something that happens to me I've learned when I use jsfiddle to create a script, then copy and paste it to a text editor and try to run it in a browser. It's a hidden character that I believe on a Mac appears as a bullet point dot kind of. – jaredwilli Nov 24 '12 at 03:32
  • I used jsFiddle to write the javascript and testing then i copied the content into the .js file then at the end it has put ? charactor but it was not visible in visual studio editor copied into Notepad++ then i saw that charactor. do the same you should be able to get rid of it. – Desmond Dec 17 '12 at 05:51
  • 2
    Got the same problem with unicode character LINE SEPARATOR value 2028. I used the online tool at http://rishida.net/tools/conversion/ to find out which hidden unicode character was possibly in my string. – Benjamin Piette Jul 01 '13 at 10:03
  • Additionally you could use text editors like Notepad++ and to find unwanted characters. – QMaster Jun 07 '14 at 09:05
  • I had some blank lines in one of my files called with AJAX. My IDE is phpstorm. I just deleted the trailing blank lines and my problem was solved. THANK YOU! – Chris Dec 26 '14 at 16:29
  • Thanks..it help me lot.There are issue in space – Amit Bera Nov 03 '15 at 20:11
  • 2
    Copy and paste from pdf file gives this error. Mine was using this quotation marks---> “ ” instead of " ". – Deke Sep 25 '16 at 04:08
  • 1
    @Deke that'll do it :) – Pointy Sep 25 '16 at 04:08
14

I get the same error in Chrome after pasting code copied from jsfiddle.

If you select all the code from a panel in jsfiddle and paste it into the free text editor Notepad++, you should be able to see the problem character as a question mark "?" at the very end of your code. Delete this question mark, then copy and paste the code from Notepad++ and the problem will be gone.

KurtWM
  • 191
  • 1
  • 6
11

I had the same error when multiline string included new line (\n) characters. Merging all lines into one (thus removing all new line characters) and sending it to a browser used to solve. But was very inconvenient to code.

Often could not understand why this was an issue in Chrome until I came across to a statement which said that the current version of JavaScript engine in Chrome doesn't support multiline strings which are wrapped in single quotes and have new line (\n) characters in them. To make it work, multiline string need to be wrapped in double quotes. Changing my code to this, resolved this issue.

I will try to find a reference to a standard or Chrome doc which proves this. Until then, try this solution and see if works for you as well.

esengineer
  • 9,514
  • 7
  • 45
  • 69
  • What about strings delimited with backtick? – Adam Bittlingmayer Oct 06 '17 at 10:39
  • Backticks, i.e. template literals, were introduces quite recently in ECMAScript 6. At the time of writing (2012), there were no backticks. But, now, yes, they should work, if the browser does support. This means, backticks are not cross browser compatible, meaning still risky. There are many users still using old desktop and mobile browsers. I would suggest to continue using double quotes. – esengineer Oct 06 '17 at 17:29
2

I had the same error in Chrome. The Chrome console told me that the error was in the 1st line of the HTML file.

It was actually in the .js file. So watch out for setValidNou(1060, $(this).val(), 0') error types.

Dragos Durlut
  • 8,018
  • 10
  • 47
  • 62