1

I have the following jsp form:

<form action = "dt.jsp" METHOD = "GET" ONSUBMIT="return validateForm()">
   <table>
      <tr>
        <td><input type=date name="fdate"/></td>
        <td><input type=date name="tdate"/></td>
    </tr>
</table>
<input  TYPE = "SUBMIT" VALUE = "Search by date">
</form>

and javascript function:

function validateForm()
{
    alert(document.getElementsByName('fdate').value);
    return false;
}

when I do the alert I get undefined. why?

1 Answer 1

2

document.getElementsByName('fdate') returns an array, or more accurately a NodeList.

Use document.getElementsByName('fdate')[0].value

See https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByName

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. now I try to compare this 2 dates like this: function validateForm() { if(dates.compare(document.getElementsByName('fdate') [0].value, document.getElementsByName('tdate')[0].value) == -1) { alert("to date must be bigger then from date"); return false; } } what is the problem?
@user3557163 You have to ask a new question. At StackOverflow, we have one issue per question to keep questions more useful to other users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.