Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement

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.

 

 

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