This demonstrates how to validate a user's input using SQL Validation. This functionality allows you to execute a SQL statement to determine if the user's input is valid, and provide immediate feedback if it is not.
In order to implement this functionality, we will do the following:
Create the Question
1) Create the question that will accept the user's input and set the following properties:
- Give it the name "Product"
- Give it the shortname "ProductName"
- Set the "Question Type" to "Textbox"...Singleline textbox (Short)
- If the field will be required, then set it's properties accordingly under the "Question Validation" settings.
Setup the SQL Validation
2) Under "Module Configuration" > "Validation Configuration" set the following properties:
- Check the "Enable Server-side Validation" checkbox
- Check the "Set focus on first validation error field checkbox
- Check the "Enable custom SQL validation script" checkbox.
- In the "Validation SQL Query" textbox enter the SQL statement or Stored Procedure name to execute to perform the validation.
- Below is the SQL Stored Procedure created for this demonstration (this was created one time under Host, SQL):
- CREATE PROCEDURE [dbo].[stp_SQLValidation]
SELECT COUNT(*) As IsValid
WHERE Name = @ProductName
- Below is the SQL that is called from within the 'Validation SQL Query' within this demonstration (this is whats entered into this form setting within the module configuration, validation, Validation SQL Query textbox):
- stp_SQLValidation '$(ProductName)'
- Note: In order to prevent a possible SQL Injection attack, it is recommended the SQL validation be performed using a stored procedure.
- In the "Validation Error Message" textbox, enter the message to be displayed to the user in the event the SQL validation has failed.
In the above demonstration, if a user enters an invalid product name into the textbox a message will be displayed stating There were no products that matched your entry. If however, a valid product name is entered, such as "AWC Logo Cap", the form will submit as usual and you will be redirected to the "thank you" page.
See other Form Demos