Adding Validation to a TextField
Text fields are used often in Flash Remoting applications, because you frequently need to accept user input for your remote calls. Any time a user can interact with an application, the possibility exists for major problems. One way to minimize the types of problems you might encounter is to validate the user-input data before it is sent to the remote server.
Warning
Client-side validation is important for a smooth user experience, but you should also validate user-input data on the server, to avoid malicious attacks on your server-side services. Remember, when you set up a remote service, it is open to the world, whether a user uses your Flash interface or not.
The next example shows how you might implement a validation routine directly on the TextField class. (The TextField component on the DRK 3 CD-ROM also features validation functionality.) Simply calling TextField.validate( ) causes all text fields that have a defined validator to be validated. The method returns an error message if there is a problem, or nothing if everything validates properly. First, enhance the TextField class to keep track of the validators for each TextField instance. The validators are stored in an array:
TextField.validators = new Array( );
Next, insert a routine to allow the programmer to set a specific validator type on a given text field:
TextField.prototype.setValidationType = function (theType, errMsg) { var validator = new Object( ); validator.textfield = this; ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access