Enhancing Validation with JavaScript ColdFusion Help

The Cold Fusion validation attributes you have looked at so far have addressed only forcing a field to be required and checking the data provided in text fields. At other times, however, it may be necessary to validate the data provided in other fields. For instance, you may need to check that certain combinations of dat  in check boxes, radio buttons, select lists, or sliders are provided correctly by the user. In these instances, you need to do some manual [avafcript programming to implement client-side validation, However, ColdFusion provides two attributes that help enhance the process: ONVALI{)ATE and ONERROR.

The ONVALIDATE attribute is used with CFINPUT, C~GRID, CFSLIDER, CFTEXTINPUT I and CFTREE to specify the name of a JavaScript function of your own creation to handle input validation. You won’t look at the details of writing the javaScript here but will simply outline the information needed to implement JavaScript validation functions, given knowledge of JavaScript.

The function will be passed three objects: the form object, the field object being validated, and the value of the field object being validated. This means a function can be written that works entirely independently of the form itself because it has all the information it needs available to it.

Therefore, in writing a function for use with ONVALIDATyEo,u must write the relevant function definition to take into account the three objects being passed to it. An example is the following:

Function FieldTest(TheForm,TheField,TheValue)
Validation Code Goes Here

Consider our simple form. What you haven’t been able to test using the other validation attributes is the validity of the e-mail address if one is entered. You could use ONVALIDATE to play that role. First, you specify the name of a function to call in the appropriate CFINPUS:

If the e-rnail address meets the specification, True is returned; other-vise, False is returned. If False is the result, the default ColdFusion error alert dialog box is displayed.

In addition to supporting a custom -function for validation, you can specify a custom function ‘to execute if validation fails, This is done using the ONERROR attribute with the CFGRID, CFINPUT, CFSELECT, CFSLIDER, CFTEXTINPUT, and CFTREE tags. Functions designed for use with ONERROR ‘ Work in much the s,ameway as those with ONVALIDATE except that one extra attribute is passed to the functiorf the error message associated with the error. For instance, if you want a custom function to handle errors generated in our preceding example, you can add the ONERROR attribute to the CFINPUT tag:

Where Do We Go from Here?

In this chapter, you finished looking at forms and the ways in which ColdFusion enhances them, making them more programmer and user friedly. Going forward, you will move on to an important topic: the Web Application Framework. ColdFusion offers more than just the capability to develop individuai, interactive templates. It also offers the capability to build applications out of collections of templates. Using the Web Application Framework, these sets of templates can work together in an integrated fashion to develop anything from complete Internet commerce systems, .including shopping carts and account management, to a functional guest book system. In the next chapter, you will look at all the major aspects of the Web Application Framework, including application-level settings and client-state management.

Posted on November 14, 2015 in Validating From Data

Share the Story

Back to Top
Share This