Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Form-Building Routines III—the Conclusion  : Page 6

Use automatic control-building routines to create and populate checkbox and radio button controls from a database.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Adding Client Side Validation
Automatically generating the client side JavaScript validation code for text boxes is relatively simple, but is slightly more complex for checking a group of radio buttons. You must iterate through the button group looking for one where the checked property is true, for example:

   var blnq5Selected = false;
   for(var i in f.q5) {
      if(f.q5[i].checked) {
         blnq5Selected = true;
   if(!blnq5Selected) {
      msg += 'Please select a value for the \'Q5\' field.\n';
      retVal = false;
If the user neglected to select a radio button the script displays an error message.

Here's the ASP code that generates the client side script. You should place this code at the end of the radio and check box generation routines:

   If blnRequired Then
      mstrFormValidationScript = _
         mstrFormValidationScript & _
         "var bln" & strFieldName & _
         "Selected = false;" & vbCrLf & _
         "for(var i in f." & strFieldName & _
         ") {" & vbCrLf & _
         "   if(f." & strFieldName & "[i].checked) {" & _
         vbCrLf & "      bln" & strFieldName & _
         "Selected = true;" & vbCrLf & _
         "   }" & vbCrLf & "}" & vbCrLf & _
         "if(!bln" & strFieldName & _
         "Selected) {" & vbCrLf & _
         "   msg += 'Please select a value for the \'" & _
         strLabel & "\' field.\n';" & vbCrLf & _
         "   retVal = false;" & vbCrLf & _
         "}" & vbCrLf & vbCrLf 
      End if   
You can download the code for all these routines from the Resources section of this article. To use the code, use an INCLUDE command to insert the file "IncFormRoutines.asp" in your ASP page:

   <!--#include file="IncFormRoutines.asp" -->

Rama Ramachandran is Vice President of Technology with Imperium and is a Microsoft Certified Solution Developer and Site Builder. He has extensive experience with building database systems and has co-authored several books including Professional VB.Net, Introducing .Net, and Professional ASP Data Access (Wrox). Rama also teaches Visual Basic and Web Development at Fairfield University and University of Connecticut.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date