View Full Version : Form Field Comparison

08-02-2006, 08:37 PM
Hi guys I am new to javascript!

I have three fields that I want validated on a form. Item3 cannot be smaller than Item2 and Item2 cannot be smaller than Item1.

Below is what I had in mind:

Function validateForm()
If (Item3 < Item2)
alert="You must enter a bigger size for Item 3"
If (Item2 < Item1)
alert="You must enter a bigger size for Item 2

<form method="POST" action="Post.jsp" onsubmit="validateForm(this)">

08-02-2006, 09:26 PM
1. elements(objects) should be properly refered. A form's element is to be found, usually (but there are some other methods as well) as an element of the form


2. fields' input content is controlled by the value attribute. this attribute returns always a string, not a number, so that before any Math operation or compare, the return result must be transformed in a number. One of the methods is Number().

3. Usualy, the validate follows this general pattern:

function validate(){
if(... condition is not fulfiled...){
alert('Hey, the condition is not fulfiled!')
return false;
<form onsubmit="return validate()">

Now try to play with this and let us know what you have done.

08-02-2006, 10:01 PM
Hmm very interesting. Why was this posted twice?

08-02-2006, 11:00 PM
Do you have any links to online resources regarding this subject?

08-02-2006, 11:05 PM
the usual javascript online tutorials... Nothing special.