Javascript

I went for an interview for a job one aspect of which was web design, and was tested to see if I could write javascript validation for a form. Now, I suprised myself to find that I couldn't get a simple piece of code to work and do what was required when I was put on the spot.


The fact is, that although, in the past, I have played around a lot with javascript actions on forms, I eventually lost enthusiasm for client side scripting altogether. (One example of client side scripts you can see at the site http://www.nascroydon.org.uk click on Newsletter archive and choose an item from one of the pull down lists.)


My big problem with client side javascript is:


  • It doesn't work on all browsers, leaving users potentially unable to use your site
  • Malicious users can override your validation by recoding the script, so to be secure you have to code the same logic server side anyway


Now it has been taught to me that there are good uses for client scripting, for example, warning the user before they delete a record, to make sure they haven't done it by accident.

e.g.

onClick="return (window.confirm('are you sure'));"


What I have taken to heart is that a page must function whether or not javascript is enabled, but that it can provide an enhancement to the users experience.

Coming back to the question of how to quickly check whether a field has been filled in on the client side, this can be done in the onClick event of the submit button, which must return true or false depending on whether the input is valid.

In the case of my interview, the code I typed was:

onClick="if (form1.textfield=='') window.alert('you must enter some text');"

the correct code would have been:

onClick="if (document.form1.textfield=='') window.alert('you must enter some text');"

What the incident really brought home to me is how much I depend on using a search engine to find snippets of working code, without which it is easy to forget the exact keywords and syntax required.