GEHC JS Class 3 About Forms ----------- Forms are an essential part of any interactive site - they allow you to give the user a chance to fill in data specific to them, whether it is a name, address, credit card number, color preference, or email address. In addition, we can use forms to build pages that gather data on one page and pass it along to many other pages. We will leverage this ability in the very last week of class, after we learn about Perl. In the meantime, JavaScript goes hand in hand with almost every form that I build on the web. There are lots of reasons why, which we'll touch on today. Forms and data validation ------------------------- Validation of data placed in forms by users is critical. It must be done before the server tries to store the data, or else the data will be useless. We can validate data on the server, but that's our last line of defense and the one that is the least useful to users. What users want is instant feedback about what they have done wrong. Using JavaScript, we can validate user input before it ever leaves the client, providing instant feedback to the user, and sparing our server a hit for simple data validation. Is it blank? ------------ One of the first tests we will want to do is find out if a data field is blank or not. Very often, there are required fields in a form, and we will make sure we are able to test those fields to see if they are blank. ** JS3-1 - a simple text box, with a first attempt at blankness testing. This testing is broken, because it is too simple. If the user types nothing in the box and presses Submit, it will tell them the box is empty. If they type anything else in the box, it will say it is not empty... but if they type in spaces, then that passes the != "" test, but is obviously wrong. We need a function to test data for blankness. *TIME OUT* Let's take a few moments to look at the HTML and the corresponding JS in this example. In the HTML, we have a form:
This form tag is very basic, and includes a call to a new event, the onSubmit event. The onSubmit event fires when the submit button is pressed. In other words, when the user thinks they are done with the form, and we should start checking data, the event fires. In this case, when the event fires, we call the checkForm() function, previously declared in the head. In the checkForm() function, we have this code: if (document.forms[0].elements[0].value == "") { Here we see the document object, and a new property of the object, the forms array. Typically, there is only one form on a page, so referring to forms[0] is much safer than referring to images[0]. Within a Form object, there is the elements property. There is one member of the elements property for each "widget" in the form (a widget is a text box, button, radio button, etc). Using the index number here is as tricky as with the images array. Each of the different kinds of elements has a different set of methods and properties, too. Here, we want to see the data inside the text box on the screen. That is the value property of the 0th element of the 0th form of the document. We can examine the data, to test to see if it is not equal to something or, in this case, if it is equal to something specific. If the value in the box is "" (empty string) then we put up an alert box indicating the box is empty. Otherwise, we put up an alert box saying it is filled in. Just as we named the images before, we can also name the forms and the widgets to make our lives easier. Doing so will make our code more robust and make it much easier to read. I name almost every one of my forms "theform" to make my code generally more consistent, but you can name them what ever you wish. Naming widgets is a little more important, because you want to pick something short and descriptive, like "name," "address," or "city." Here I've used a generic name, inputbox. Now in my JS code, I can say: if (document.theform.inputbox.value == "") { One other point to bring up, which just reinforces our discussion of return values from last week. Note that the onSubmit event returns the value of checkForm(). Note that checkForm() always returns false. This is perfect for our needs - when the onSubmit function returns true, the form is actually submitted. Here, until we learn about the server side, we always want to return false, to keep the form from submitting. When we return false, the submission itself is suppressed. ** JS3-2 - second try, with object names and an isblank function Here I've named the objects, but I've also changed my checkForm() function to call another function to do blankness testing. The isblank function is one we will use a lot. if (isblank(document.theform.inputbox.value) == true) { We have here a call to our isblank function. Based on its name, we can presume that it returns a true if the data is blank, and a false if it is not blank. We also see that we pass it one value, a variable to check. In this case, the variable is the inputbox's contents, stored in inputbox.value. In isblank, we're doing a couple if things. if (value.length == 0) { return true; } First, we check to see if the field is truly blank - in other words, is it an empty string. Rather than use the == "" test, we use a property of a string object, the length property. I've found it to be more robust than the == "" test. So, if the value.length is 0, then it is blank, and we return true. If the string does not have a length of 0, our next task is to scan as much of the string as we have to, to find out if there are any non-blank characters. for (i=0; i