Illegal Characters in jQuery

Added on: Wednesday 14th September 2011

I was caught out the other day by some jQuery form validation not working on one of CMS sites. The reason, I eventually found out was an illegal character in an element ID.

Our CMS includes a form builder that allows users to add contact forms, surveys etc to their websites.

The form builder generates the html to go on the page and users the jquery validate and metadata plugins to do the validation.

The validate plugin works by running the validate() function on a form element when the page loads. for example:

$("#form").validate();

Now as it happens, the form builder creates the 'form id' from the name of the form as defined by the user - after stripping out illegal characters of course.

I spent hours trying to find out why the javascript validation on this particular form wasn't working and eventually traced it to the fact the the 'form id' contained ( and ) characters.

The form builder wasn't stripping these out and there was no error in the browser - I guess jQuery just wasn't finding the element at all.

I've altered the form builder now but just thought this might be useful for anyone else having problems.