Validating user input in perl
For functions that you use often, you can go a step further and place these in a Java Script file that you can include in multiple HTML files. can perform data validation is to do so just before the form is submitted.
This is the best time to check whether required fields have been filled or to perform checks that involve dependencies between multiple elements.
As we mentioned earlier, Java Script may be supported by the user's browser or it may be turned off.
Thus, you cannot rely on Java Script validation being performed.
The benefit Java Script provides for the server is that it shifts some work that might otherwise be done on the server to the client, and it reduces the number of server requests.
The benefit Java Script provides to the user is that it provides immediate feedback without a delay while the browser fetches a new page.
We perform this check with Java Script's // form // Common functions used with forms // // We use this as a hash to track those elements validated on a per element // basis that have formatting problems validate = new Object( ); // Takes a value, checks if it's an integer, and returns true or false function is Integer ( value ) // Takes a value and a range, checks if the value is in the range, and // returns true or false function in Range ( value, low, high ) // Checks values against formats such as '#####' or '###-##-####' function check Format( value, format ) // Takes a form and an array of element names; verifies that each has a value function require Values ( form, required Values ) // Takes a form and an array of element names; verifies that each has an // option selected (other than the first; assumes that the first option in // each select menu contains instructions) function require Selects ( form, required Select ) // Takes a form and an array of element names; verifies that each has a // value checked function require Radios ( form, required Radio ) // Verify there are no uncorrected formatting problems with elements // validated on a per element basis function check Problems ( ) // Verifies that the value of the provided element has ##### format function check Zip ( element ) // Verifies that the value of the provided element has ###-###-#### format function check Phone ( element ) // Verifies that the value of the provided element has ###-##-#### format function check SSN ( element ) // Verifies that the value of the provided element is an integer between 1 and 150 function check Age ( element ) takes a value as well as a string containing a format to check the value against.
The structure of our format string is quite simple: a pound symbol represents a numeric digit and any other character represents itself.
For example, if a field only accepts numbers, you can verify that the user did not enter any non-numeric characters. It then returns true or false depending on whether the check was successful or not.For example, some of the fields may only accept numbers while others may only accept dates, some fields may only accept a certain range of entries, some fields may be required, and some combinations of fields may not permitted.All of these examples must be handled by only two types of checks: the first is to validate each element user's input as the data is entered; the second is to perform the validation when the form is submitted.Second, we gain much by doing Java Script validation because it lets us validate as close to the user as possible.If we perform data validation on the client using Java Script, we avoid unnecessary network connections because if Java Script notices an invalid entry, it can immediately notify the user who can correct the form before it is submitted.
This isn't necessary, but it does help us if we later decide to string multiple function calls together as you'll see later in Example 7-2. However, it's often much easier to work with HTML documents when the Java Script is kept together as much as possible, and functions help us to do this.