[JScript] Validation Nightmare

Results 1 to 3 of 3

Thread: [JScript] Validation Nightmare

  1. #1
    Join Date
    Dec 1969

    Default [JScript] Validation Nightmare

    Hi,<BR>Sorry for the OT post, but I know this is a good forum, so I thought I&#039;d try my luck. I&#039;ve got a very large form to validate, one with lots of radio buttons on it. I had a quick go at writing some sort of routine that would check each form element in turn, check it&#039;s type and apply some sort of validation appropriate to that type. I&#039;ve been caught out by the fact that radio buttons count as one element per button, not per group, so I can&#039;t really do that. Can anyone suggest an approach to doing this which would mean I don&#039;t have to validate each and every radio group individually?

  2. #2
    Join Date
    Dec 1969

    Default RE: [JScript] Validation Nightmare

    Try this bit of code... but you&#039;ll need to modify it:<BR><BR>&#060;script language="JavaScript"&#062;<BR><BR> <BR>function submitForm(){<BR>document.surveyform.submit();<BR> }<BR><BR>function checkWholeForm(){<BR>var thisquestion = 0;<BR>var element;<BR>var warn = &#039;&#039;;<BR>var warncount = 0;<BR>for (j=0;j&#060;30;j++){<BR> element = eval(&#039;document.surveyform.q&#039; + (j+1));<BR> if (typeof element == &#039;object&#039;){<BR> //alert(element.length);<BR> thisquestion = 0;<BR> for (i=0;i&#060;element.length;i++){<BR> if (element[i].checked == true) thisquestion=1;<BR> }<BR> if (thisquestion==0){warn = warn+&#039; &#039;+(j+1);warncount=warncount+1}<BR> }<BR>}<BR><BR>if (warncount == 1) {<BR> alert("you have missed out question "+warn+"
    All questions need to have at least one answer");<BR> return false;<BR> }<BR>if (warncount &#062; 1) {<BR> alert("you have missed out questions: "+warn+"
    All questions need to have at least one answer");<BR> return false;<BR> } <BR><BR>submitForm();<BR>}<BR> <BR><BR> <BR>&#060;/script&#062;<BR><BR>Now modify your submit button with this code:<BR><BR>&#060;input type="button" name="submitbutton" value="Submit Feedback" onClick="checkWholeForm()"&#062;<BR><BR>This validation script will work with any number of form elements... look at the code and you should be able to pickup how to modify it to suite. If not repost and let me know.<BR><BR>James

  3. #3
    Join Date
    Dec 1969

    Default Have a read of this...

    This is the script that I use to validate forms with, it&#039;s simple, and re-useable:<BR><BR>-----------------------------------------<BR><BR>http://www.flws.com.au/showusyourcode/codeLib/code/dynamicFormValidation.asp?CatID=2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts