views:

57

answers:

2

Hi All, I have a JSP page where I load an arraylist which has the list of fields to be displayed.If the arrayList has nothing I will not load any inputbox else create as many input boxes as there are in arraylist which is done correctly.I keep a name field1,field2,field3...fieldn where n is the size of the array.Now When I submit my form I should check whether the value of these fields are not empty or null and should alert the user that if the fields are blank.I can use only js and no RIA or AJAX.

Can anyone help me in this issue?

A: 

Uhm,

if (field_whatever.value.length == 0)
{
alert('field_whatever is empty!');
}

Or did I missunderstand your question?

Jonas B
U misunderstood my question.It is like how do we access the individual element from the form.Is there a way to access the particular element field1 or field2 dynamically based on the size.<%{%><INPUT maxLength=50 type ="text" name=<%=fieldArray.next()%> size=30 value=""><%}%>when the user submits the form I should check each and every element and check whether it is null
Harish
A: 

You can give each input field an ID when creating them in your loop:

<input type="hidden" id="fieldSize" value="<%=fieldList.size()%>" />
<%
  int i = 0;
  for (String fieldName : fieldList) {
%>
  <input id="field-<%=i++%>"/>
<%
  }
%>

Then in JavaScript create a function which is called on submit (onsubmit) which goes through the list of fields and checks whether they are empty or not. If non were empty return true, otherwise return false.

function checkFields() {
  var size = parseInt(document.getElementById("fieldSize").value);
  for (var i = 0 ; i < size ; i++) {
    if (document.getElementById("field-" + i).value.length == 0) {
      return false;
    }
  }
  return true;
}

This is just a simple structure of how to achieve this. I'll leave the rest (showing error messages etc.) to you.

Wesho
thanks that really helped
Harish