Javascript Tips and Tricks

How to Validate a Web Form with JavaScript by Kelly Lynn

If you’re building a website, at some point, you’ll probably need a form so users can submit data. Forms are easy to build in HTML and you can add some simple JavaScript to your page to make sure that users submit valid data in the form fields. Let’s take a look at how to use JavaScript to validate some commonly used form fields. The HTML Form First we’ll create a basic form in HTML. Users will enter a name, email address, and age on the form: The HTML code for the form is very simple: <form id=”SampleForm” onsubmit=”validateForm();”> Name: <input type=”text” id=”SubscriberName”><br> Email: <input type=”text” id=”Email”><br> Age: <input type=”text” id=”Age”><br> <input type=”submit” value=”Submit”> </form> The JavaScript Function Now we need to add a JavaScript function to make sure that the … Continue reading

Cross-Origin Resource Sharing by Michel Plungjan

Background For a long time developers pulled their hair out when attempting to get Firefox (Fx) to allow a script from one domain to read a file from another domain via XMLHttpRequest. The two most common solutions I saw and used were: inserting a script which returns JSON (JSONP) using a proxy on one domain to connect to another domain which fetches JSON. IE is less choosey, and on an intranet, one can simply add the foreign site as 'trusted' and simply go get the data. Woot! In June 2009, with the release of Fx 3.5, a small change suddenly made a lot of hacks redundant: have the target server add a simple header to their response, and Fx will happily accept the data. Mozilla.org had implemented the client side … Continue reading