• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Password Validate

I'm trying to setup a script to validate a password I believe I have it setup correctly but it is not working.  Below is the full script.  Can somebody tell me if I am missing something? The form inserts everything into the db fine.

<SCRIPT language=javascript type=text/javascript>
<!--

function validate(form1) {
      if (document.form1.LastName.value.length < 2) {
            alert("Please enter your Last Name.");
            return false;
      }
      if (document.form1.FirstName.value.length < 2) {
            alert("Please enter your First Name.");
            return false;
      }
      if (document.form1.Address.value.length < 3) {
            alert("Please enter your Address.");
            return false;
      }
      if (document.form1.City.value.length < 3) {
            alert("Please enter your City.");
            return false;
      }
      if (document.form1.State.value.length < 3) {
            alert("Please enter your State.");
            return false;
      }
      if (document.form1.Zipcode.value.length < 5) {
            alert("Please enter your Zipcode.");
            return false;
      }
      if (document.form1.Phone.value.length < 10) {
            alert("Please enter your Phone Number.");
            return false;
      }
      if (document.form1.email.value.length < 8) {
            alert("Please enter your Email Address.  This will be used as your user ID");
            return false;
       }
        if (form1.Password.value == "" || form1.retype.value == "") {
                alert("Password can not be empty");
                return false;
         }
        if (form1.Password.value != form1.retype.value) {
               alert('The passwords do not match, try again!');
               form1.Password.value='';
               form1.retype.value='';
               form1.Password.focus();
                return false;
         }
      return true;
}
       

//-->
</SCRIPT>

<CENTER>
<FORM name=form1 onsubmit="return validate(form1);" action=Webpage method=post>
<TABLE>
<TBODY>
<TR>
<TD align=right><STRONG>Last Name</STRONG></TD>
<TD><INPUT id=LastName size=30 name=LastName></TD></TR>
<TR>
<TD align=right><STRONG>First Name</STRONG></TD>
<TD><INPUT id=FirstName size=30 name=FirstName></TD></TR>
<TR>
<TD align=right><STRONG>Address</STRONG></TD>
<TD><INPUT id=Address size=30 name=Address></TD></TR>
<TR>
<TD align=right><STRONG>City</STRONG></TD>
<TD><INPUT id=city size=30 name=City></TD></TR>
<TR>
<TD align=right><STRONG>State</STRONG></TD>
<TD><INPUT id=State size=30 name=State></TD></TR>
<TR>
<TD align=right><STRONG>Zipcode</STRONG></TD>
<TD><INPUT id=Zipcode size=30 name=Zipcode></TD></TR>
<TR>
<TD align=right><STRONG>Phone Number</STRONG></TD>
<TD><INPUT id=phone size=30 name=Phone></TD></TR>
<TR>
<TD align=right><STRONG>Email Address</STRONG></TD>
<TD><INPUT id=email size=30 name=Email></TD></TR>
<TR>
<TD align=right><STRONG>Password</STRONG></TD>
<TD><INPUT id=password type=password size=30 name=Password></TD></TR>
<TR>
<TD align=right><STRONG>Re-type</STRONG> <STRONG>Password</STRONG></TD>
<TD><INPUT type=password size=30 name=Retype ip="retype"></TD></TR>
<TR>
<TD colSpan=2>
<P align=center><INPUT type=submit value=Submit> </P></TD></TR></TBODY></TABLE>
<P </FORM></CENTER></TD></TR><TR><TD align="right"></TD><TD width="100%"></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
0
Cveselka
Asked:
Cveselka
2 Solutions
 
ZvonkoSystems architectCommented:
JavaScript is CaseSensitive.
And you mismatched field names in HTML and JavaScript: Retype and retype
Also you added some attribute ip= which perhaps should be id=

0
 
knightEknightCommented:
either change the name of the field from "Retype" to "retype"
or change your code:


        if (form1.Password.value != form1.Retype.value) {

0
 
Cem TürkSenior Software EngineerCommented:
i have tested the following on FF and IE it works nice, i made a few corrections related with the field retype

<SCRIPT language=javascript type=text/javascript>
<!--

function validate(form1) {
     if (document.form1.LastName.value.length < 2) {
          alert("Please enter your Last Name.");
          return false;
     }
     if (document.form1.FirstName.value.length < 2) {
          alert("Please enter your First Name.");
          return false;
     }
     if (document.form1.Address.value.length < 3) {
          alert("Please enter your Address.");
          return false;
     }
     if (document.form1.City.value.length < 3) {
          alert("Please enter your City.");
          return false;
     }
     if (document.form1.State.value.length < 3) {
          alert("Please enter your State.");
          return false;
     }
     if (document.form1.Zipcode.value.length < 5) {
          alert("Please enter your Zipcode.");
          return false;
     }
     if (document.form1.Phone.value.length < 10) {
          alert("Please enter your Phone Number.");
          return false;
     }
     if (document.form1.email.value.length < 8) {
          alert("Please enter your Email Address.  This will be used as your user ID");
          return false;
      }
        if (form1.Password.value == "" || form1.Retype.value == "") {
                alert("Password can not be empty");
                return false;
         }
        if (form1.Password.value != form1.Retype.value) {
               alert('The passwords do not match, try again!');
               form1.Password.value='';
               form1.Retype.value='';
               form1.Password.focus();
                return false;
         }
     return true;
}
       

//-->
</SCRIPT>

<CENTER>
<FORM name=form1 onsubmit="return validate(form1);" action=Webpage method=post>
<TABLE>
<TBODY>
<TR>
<TD align=right><STRONG>Last Name</STRONG></TD>
<TD><INPUT id=LastName size=30 name=LastName></TD></TR>
<TR>
<TD align=right><STRONG>First Name</STRONG></TD>
<TD><INPUT id=FirstName size=30 name=FirstName></TD></TR>
<TR>
<TD align=right><STRONG>Address</STRONG></TD>

<TD><INPUT id=Address size=30 name=Address></TD></TR>
<TR>
<TD align=right><STRONG>City</STRONG></TD>
<TD><INPUT id=city size=30 name=City></TD></TR>
<TR>
<TD align=right><STRONG>State</STRONG></TD>
<TD><INPUT id=State size=30 name=State></TD></TR>
<TR>
<TD align=right><STRONG>Zipcode</STRONG></TD>
<TD><INPUT id=Zipcode size=30 name=Zipcode></TD></TR>
<TR>
<TD align=right><STRONG>Phone Number</STRONG></TD>
<TD><INPUT id=phone size=30 name=Phone></TD></TR>

<TR>
<TD align=right><STRONG>Email Address</STRONG></TD>
<TD><INPUT id=email size=30 name=Email></TD></TR>
<TR>
<TD align=right><STRONG>Password</STRONG></TD>
<TD><INPUT id=password type=password size=30 name=Password></TD></TR>
<TR>
<TD align=right><STRONG>Re-type</STRONG> <STRONG>Password</STRONG></TD>
<TD><INPUT type=password size=30 name=Retype id="retype"></TD></TR>
<TR>
<TD colSpan=2>
<P align=center><INPUT type=submit value=Submit> </P></TD></TR></TBODY></TABLE>

<P ></FORM></CENTER></TD></TR><TR><TD align="right"></TD><TD width="100%"></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Cem TürkSenior Software EngineerCommented:
Zvonko and  knightEknight sorry forgot to refresh the page before posting....
0
 
HonorGodCommented:
<html>
<head>
<script language='javascript' type='text/javascript'>
<!--
  function validate( form1 ) {
    if ( document.form1.LastName.value.length < 2 ) {
      alert("Please enter your Last Name.");
      return false;
    }
    if (document.form1.FirstName.value.length < 2) {
      alert("Please enter your First Name.");
      return false;
    }
    if (document.form1.Address.value.length < 3) {
      alert("Please enter your Address.");
      return false;
    }
    if (document.form1.City.value.length < 3) {
      alert("Please enter your City.");
      return false;
    }
    if (document.form1.State.value.length < 3) {
      alert("Please enter your State.");
      return false;
    }
    if (document.form1.Zipcode.value.length < 5) {
      alert("Please enter your Zipcode.");
      return false;
    }
    if (document.form1.Phone.value.length < 10) {
      alert("Please enter your Phone Number.");
      return false;
    }
    if (document.form1.email.value.length < 8) {
      alert("Please enter your Email Address.  This will be used as your user ID");
      return false;
     }
    if (form1.Password.value == "" || form1.retype.value == "") {
      alert("Password can not be empty");
      return false;
    }
    if (form1.Password.value != form1.retype.value) {
      alert('The passwords do not match, try again!');
      form1.Password.value='';
      form1.retype.value='';
      form1.Password.focus();
      return false;
    }
    return true;
  }
//-->
</script>
</head>
<body>
<center>
  <form name=form1 onsubmit="return validate(form1);" action=Webpage method=post>
    <table>
      <tbody>
        <tr>
          <td align=right><strong>Last Name</strong></td>
          <td><input id=LastName size=30 name=LastName></td></tr>
        <tr>
          <td align=right><strong>First Name</strong></td>
          <td><input id=FirstName size=30 name=FirstName></td></tr>
        <tr>
          <td align=right><strong>Address</strong></td>
          <td><input id=Address size=30 name=Address></td></tr>
        <tr>
          <td align=right><strong>City</strong></td>
          <td><input id=city size=30 name=City></td></tr>
        <tr>
          <td align=right><strong>State</strong></td>
          <td><input id=State size=30 name=State></td></tr>
        <tr>
          <td align=right><strong>Zipcode</strong></td>
          <td><input id=Zipcode size=30 name=Zipcode></td></tr>
        <tr>
          <td align=right><strong>Phone Number</strong></td>
          <td><input id=phone size=30 name=Phone></td></tr>
        <tr>
          <td align=right><strong>Email Address</strong></td>
          <td><input id=email size=30 name=Email></td></tr>
        <tr>
          <td align=right><strong>Password</strong></td>
          <td><input id=password type=password size=30 name=Password></td></tr>
        <tr>
          <td align=right><strong>Re-type</strong> <strong>Password</strong></td>
          <td><input type=password size=30 name=Retype ip="retype"></td></tr>
        <tr>
          <td colSpan=2><p align=center><INPUT type=submit value=Submit> </p></td>
        </tr>
      </tbody>
    </table>
    <P>
  </form>
</center>
</body>
</html>

  You had some extra stuff at the end....  Specifically, the stuff after the first </table>

0
 
CveselkaAuthor Commented:
Thank you guys that was fast.  I'm new to this Web Developing and it can get frustrating and it was just a little mistake.
0
 
ZvonkoSystems architectCommented:
And if you want to improve your checking, then check this:


<SCRIPT language=javascript type=text/javascript>
<!--
var chkField = [
["LastName",/\w{2,}/,"Please enter your Last Name."],
["FirstName",/\w{2,}/,"Please enter your First Name."],
["Address",/\w{3,}/,"Please enter your Address."],
["City",/\w{3,}/,"Please enter your City."],
["State",/\w{3,}/,"Please enter your State."],
["Zipcode",/^\d{5}$/,"Please enter your Zipcode."],
["Phone",/^[\d\ \-]{10,}/,"Please enter your Phone Number."],
["email",/^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.[a-z]{2,4}$/i,"Please enter your Email Address.\nThis will be used as your user ID"],
["Password",/\w/,"Password can not be empty"],
["Retype",/\w/,"Please retype the Password."]];

function validate(form1) {
  for(var i=0;i<chkField.length;i++){
    if(!form1[chkField[i][0]].value.match(chkField[i][1])){
      alert(chkField[i][2]);
      form1[chkField[i][0]].select();
      form1[chkField[i][0]].focus();
      return false;
    }
  }
  if (form1.Password.value != form1.Retype.value) {
    alert('The passwords do not match, try again!');
    form1.Password.value='';
    form1.Retype.value='';
    form1.Password.focus();
    return false;
  }
  return true;
}
       

//-->
</SCRIPT>

<CENTER>
<FORM name=form1 onsubmit="return validate(this);" action=Webpage method=post>
<input type=button onClick="return validate(this.form);">
<TABLE>
<TBODY>
<TR>
<TD align=right><STRONG>Last Name</STRONG></TD>
<TD><INPUT size=30 name=LastName></TD></TR>
<TR>
<TD align=right><STRONG>First Name</STRONG></TD>
<TD><INPUT id=FirstName size=30 name=FirstName></TD></TR>
<TR>
<TD align=right><STRONG>Address</STRONG></TD>
<TD><INPUT id=Address size=30 name=Address></TD></TR>
<TR>
<TD align=right><STRONG>City</STRONG></TD>
<TD><INPUT id=city size=30 name=City></TD></TR>
<TR>
<TD align=right><STRONG>State</STRONG></TD>
<TD><INPUT id=State size=30 name=State></TD></TR>
<TR>
<TD align=right><STRONG>Zipcode</STRONG></TD>
<TD><INPUT id=Zipcode size=30 name=Zipcode></TD></TR>
<TR>
<TD align=right><STRONG>Phone Number</STRONG></TD>
<TD><INPUT id=Phone size=30 name=Phone></TD></TR>
<TR>
<TD align=right><STRONG>Email Address</STRONG></TD>
<TD><INPUT id=email size=30 name=Email></TD></TR>
<TR>
<TD align=right><STRONG>Password</STRONG></TD>
<TD><INPUT id=password type=password size=30 name=Password></TD></TR>
<TR>
<TD align=right><STRONG>Re-type</STRONG> <STRONG>Password</STRONG></TD>
<TD><INPUT type=password size=30 name=Retype id="Retype"></TD></TR>
<TR>
<TD colSpan=2>
<P align=center><INPUT type=submit value=Submit> </P></TD></TR></TBODY></TABLE>
<P </FORM></CENTER></TD></TR><TR><TD align="right"></TD><TD width="100%"></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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