?
Solved

form validation (text area)

Posted on 2003-12-05
5
Medium Priority
?
473 Views
Last Modified: 2010-03-31
Can data validation be done when the number of text entries is unknown?

I have a form where the number of text boxes is created by the number of destinations entered on the previous jsp.  Is there a way to do JavaScript to make sure data is entered into the textbox when I don’t know how many I will have?  I am using a JavaBean on the first JSP but not on the second.  On the second jsp I just want to make sure the text box is not blank.

<code>

  String numDest = fltReqInfo.getNumDest();
  int i = Integer.valueOf(numDest).intValue();   //number of destinations
  int j;       //loop counter
  String prompt = "";   //propmt string for desired city
  String cityPrompt = "";   //prompt for text box name

<table border="0">
<%
  for (j=1;j<=i;j++)
  {
  if (j==1)
  {
    prompt = "Starting City";
  }
  else if (j==i)
  {
    prompt = "Final City";
  }
  else
  {
    prompt = "City " + j;
  }
  cityPrompt = "city" + j;
%>
<tr>
  <td><%= prompt%></td>
  <td><input type="text" name="<%= cityPrompt %>" value="" size="30" maxlength="30" /></td>

</table>

                 <center>
                    <input type="submit" name="butNext"
                    value="Submit" /> <input type="Reset" />
                  </center>

</form>
</code>
0
Comment
Question by:wykzimme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9883274
You could name each text box starting with 'tb' for instance. Loop through the form elements and test each one for null or empty content
0
 
LVL 92

Expert Comment

by:objects
ID: 9884833
> Is there a way to do JavaScript to make sure data is entered

Your javascript is being generated by the same jsp page so you can include the number of fields.

If you are processing the form data on the server then add a hidden variable storing the number of fields.
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9886830
//function to check if field value is entered or not
function validRequired(formField,fieldLabel)
{
  var result = true;
  if (formField.value == "")
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  return result;
}


function validateForm(theForm)
{
  // Customize these calls for your form
for(int i = 0;i < theForm.length ; i++)
  if (!validRequired(theForm.name[i], theForm.name[i].value))
    return false;

   return true;
}

call this from the form as

<form name = "formA"  method = "post" action = "jsp3.jsp" onsubmit="return validateForm(this)">
</form>

Hope this helps . basically you give the JS the form name and ask it to check all fields.
Customize accroding to your needs.
Cheers!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 9888222
Try something like this in your form's onsubmit:

function validateTextFields() {
      for(i=0;i<document.forms.length;i++){
            for(j=0;j<document.forms[i].elements.length;j++){
                  var formObj=document.forms[i].elements[j];
                  if(formObj.type.toLowerCase() == "text") {
                        if (formObj.value == "") {
                              alert("Blank text field(s) found");
                              return false;
                        }
                  }
            }
      }
      return true;
}        

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9900053
8-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question