[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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>
0
jfs9900
Asked:
jfs9900
  • 3
1 Solution
 
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
0
 
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>
0
 
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
0
 
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now