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; ...

Get Flash Remoting: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.