Validate form elements

Results 1 to 2 of 2

Thread: Validate form elements

  1. #1
    Join Date
    Dec 1969

    Default Validate form elements

    Hello!<BR><BR>I many textareas in my ASP page. The textareas are displayed based on values from the database. Sometimes it will be 2 and sometimes 3 or 4 and so on.<BR><BR>I need to check if atleast one textarea is filled by the user. I mean user should enter values atleast in 1 textarea.<BR><BR>How do I do this. Please help.

  2. #2
    Join Date
    Dec 1969

    Default Same as any other validation

    The best thing to do is to use a regular naming convention for the textareas.<BR><BR>For example, <BR> &#060;TEXTAREA Name="Text_1"&#062;&#060;/TEXTAREA&#062;<BR> &#060;TEXTAREA Name="Text_2"&#062;&#060;/TEXTAREA&#062;<BR> &#060;TEXTAREA Name="Text_3"&#062;&#060;/TEXTAREA&#062;<BR><BR>And then you can do:<BR><BR> &#060;FORM ... onSubmit="return validate(this);"&#062;<BR><BR>&#060;SCRIPT&#062;<B R>function validate( frm )<BR>{<BR> var okay = false;<BR> for ( var tanum = 1; tanum &#060;= 1000; ++tanum ) /* 1000 is overkill */<BR> {<BR> var ta = frm.elements["Text_" + tanum];<BR> if ( ta == null ) break; // stop when no more text areas<BR> if ( ta.value.length &#062;= 10 ) /* choose your own minimum size */<BR> {<BR> okay = true; // found at least one filled in<BR> break; // no point in looking for more<BR> }<BR> } // end of for loop<BR> if ( ! okay )<BR> {<BR> alert("You must fill in at least one of the text areas");<BR> return false;<BR> }<BR> // do other validation of other fields here<BR> // ... return false on any validation failure ...<BR><BR> // if nothing failed:<BR> return true; // don&#039;t forget this!<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>

Posting Permissions

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