skip to content »

el-feel.ru

Validating user input in perl

form element when the user enters a value is most effective for validating the format or range of a particular element.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.

validating user input in perl-24

Similarly, if our CGI script then calls a database, the database will again validate the input that we sent on to it, etc.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. With Java Script, however, we can do much more on the client side. They simply accept input and pass it on to the web server where all the processing must occur.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.

They also allow us to share code when we have multiple form elements that require the same validation.

Otherwise, the form is submitted to the CGI script which handles the query like any other request.

In this case, the CGI script would record the data in a file or database.

create an HTML form, you generally expect the user to fill it out in a particular way.

There are numerous types of restrictions a form may have.

Of course, in Perl we could easily do checks like this with a regular expression.