Javascript form field validation

I have two form feilds (text box) that I want to vailidate are the same before the form is submitted. The form code looks like this:

   <form>
                       
                        <p><font size="2"><b>New password: <br>
                                    <input type="password" name="Word" size="20" class="formblue">
                                    </b></font></p>
                                    <p><font size="2"><b>Confirm new password: <br>
                                    <input type="password" name="Word1" size="20" class="formblue">
                                    </b></font>
                                    <input type="submit" value="Submit" class="submit" style="float: right"></p>
                         
                        </form>
jfs9900Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nabsolCommented:


<script language="JavaScript">
function validate()
{
  if(document.forms[0].Word.value == document.forms[0].Word1.value)
       return true;
   else
     alert("Fields dont match")
}
</script>

               <form onsubmit="return validate()" >
                       
                        <p><font size="2"><b>New password: <br>
                              <input type="password" name="Word" size="20" class="formblue">
                              </b></font></p>
                              <p><font size="2"><b>Confirm new password: <br>
                              <input type="password" name="Word1" size="20" class="formblue">
                              </b></font>
                              <input type="submit" value="Submit" class="submit" style="float: right"></p>
                         
                        </form>

By Nab
smaccariCommented:

<form onsubmit="if (this.elements['Word'].value!=this.elements['Word1'].value) {alert('Please verify your password');return false;">
 <p><font size="2"><b>New password: <br>
       <input type="password" name="Word" size="20" class="formblue">
       </b></font></p>
       <p><font size="2"><b>Confirm new password: <br>
       <input type="password" name="Word1" size="20" class="formblue">
        </b></font>
        <input type="submit" value="Submit" class="submit" style="float: right"></p>                  
 </form>
nabsolCommented:
Hi

Better Code:



<script language="JavaScript">
function validate()
{
  if(document.forms[0].Word.value == document.forms[0].Word1.value)
   {
       return true;
   }
   else
   {
     alert("Fields dont match")
     return false;
   }
}
</script>
</body>

               <form onsubmit="return validate()" >
                       
                        <p><font size="2"><b>New password: <br>
                              <input type="password" name="Word" size="20" class="formblue">
                              </b></font></p>
                              <p><font size="2"><b>Confirm new password: <br>
                              <input type="password" name="Word1" size="20" class="formblue">
                              </b></font>
                              <input type="submit" value="Submit" class="submit" style="float: right"></p>
                         
                        </form>

</body>

By Nab

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nabsolCommented:
Hi
I am making a correction in smaccari's code as well. :-)

<form onsubmit="if (this.elements['Word'].value!=this.elements['Word1'].value) {alert('Please verify your password');return false;}">
 <p><font size="2"><b>New password: <br>
       <input type="password" name="Word" size="20" class="formblue">
       </b></font></p>
       <p><font size="2"><b>Confirm new password: <br>
       <input type="password" name="Word1" size="20" class="formblue">
        </b></font>
        <input type="submit" value="Submit" class="submit" style="float: right"></p>                  
 </form>


By Nab
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.