wykzimme
asked on
form validation (text area)
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).i ntValue(); //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>
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).i
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>
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
> 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.
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.
//function to check if field value is entered or not
function validRequired(formField,fi eldLabel)
{
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.na me[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!
function validRequired(formField,fi
{
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.na
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)