Using Server-Side Form Validation

I l @ ve RuBoard

The last thing that you want is for your site pages not to work properly. You need to make sure that a user has filled out all the required fields on a form before you try to process it. ColdFusion offers a built-in way to do this. By using server-side validation, ColdFusion can check to make sure that the user has filled in all the information in the form and that the information is properly formatted.

For this task, continue to work in the form you created in the previous task.

  1. After the form field for the user name , enter this code:

     <input type="hidden" name="name_required" value="You must enter your name"> 

    By entering this hidden form field, you are letting ColdFusion know that the Name field is required and that the message "You must enter your name" should appear if the user tries to submit the form without it. You can specify any message you want in the value attribute. The message will actually appear in the form output page.

    NOTE

    There are many different types of validation rules you can apply to your form fields. "_required" makes the field required. "_date" checks for the most common date formats. "_time" looks for the most common time formats. "_float" requires a number with decimals and "_integer" looks for non-decimal numbers. "_range" validates that the value is within a range of numbers .

  2. Add the following two validation rules to your page:

     <input type="hidden" name="email_required" value="You must enter your email">  <input type="hidden" name="comments_required" value="Please enter your comment or question"> 

    Now if a user submits the form with any empty fields, he would see an error message letting him know he has left out a required piece of information.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net