Working with Forms and Regular Expressions

I’ve never enjoyed working with regular expressions. It is tedious and not often used. However, we do need to know how these work, so please stay with the material because when you need to know how to parse strings, etc. this knowledge proves invaluable.

Forms, on the other hand, are not too difficult. You will find that learning to use JS in conjunction with forms can also prove quite useful. For example, you might need to perform some dynamic operation on a Web form (joining two data fields, extracting one piece of a string, etc.), perform form validation before saving data to a database, or calculate some value based on other values entered by the customer. You can do this using JS as you will see during this tutorial’s lesson.

Keep in mind that JS treats the contents of input fields as text strings.  So, if you use the “+” sign in an operation, JS concatenates your strings and it does not perform addition!  This means that you must first convert your string to a number so that you can perform math. The Reference Window on page 767 describes how to extract a number from a text string. To convert a number to a text string you can add a text string to it (for example, “”), or you can use the String() function. The typeof() function can be used to learn if JavaScript considers a particular object type to be a text or a number.

Form validation is a process by which the server or a user’s browser checks a form for data entry errors. Further, there are two types of form validation: server-side validation and client-side validation. With the former, all of the work is done on the server (so the user’s browser sends its values to the server and when the server finishes its work, the server returns its results back to the user’s browser) whereas with the latter all of the work is done on the user’s PC (with the results being sent to the server).

Part of validation might be to pull values from form fields, which are text values. You should be familiar with (and understand) the difference between the slice(), substr(), substring() and split() methods. The examples shown on Figure 14-29 may help you better grasp how each of these methods works. Don’t forget that strings can also be formatted using styles sheets.

A regular expression is a text string that defines a character pattern. One use of regular expressions is pattern-matching, in which a text string is tested to see whether it matches the pattern defined by a regular expression. Pattern matching is just one use of regular expressions. They can also be used to extract substrings, insert new text, or replace old text (as I mentioned previously).

Regular expressions can also include symbols that indicate the number of times a particular character should be repeated. The repetition characters are listed on Figure 14-43. If you need to use one of the special reserved symbols in a regular expression, you should use an escape sequence. An escape sequence is a special command inside a text string that tells the JavaScript interpreter not to interpret what follows as a character. The character that indicates an escape sequence in a regular expression is the backslash character “\” (see Figure 14-45).

A regular expression can also be created using the object constructor, as shown in the Reference Window on page 804. There are some important differences between the two ways of creating a regular expression in JavaScript. For one, the regular expression literal is already compiled, which means that it is ready to perform pattern matching and other regular expression tasks. The new RegExp() object, on the other hand, must be compiled before it can be used. The other important difference is that the new RegExp() constructor takes a text string for its parameter value. Text strings can also contain escape sequences, which may be used to insert non-textual characters like tabs or carriage returns. To avoid conflict between escape sequences designed for text strings and escape sequences designed for regular expressions, you have to insert an additional escape character \ for each regular expression escape sequence.

The syntax of regular expressions may be intimidating (it is for me too!). The way you get these concepts is to walk through the examples and then try some of your own variations of what you see, observing what happens when you change things around. Once you learn this concept the next time around is a bit easier as you struggle to relearn it once again – like I do every year. LOL.

Leave a Reply

You must be logged in to post a comment.