password field validation - help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    #1

    password field validation - help

    I've been trying for the past week to put a simple code together. I
    have done a LOT of searching, found scripts showing the functions I
    would like to use, however when I mix them it all goes wrong, somehow
    I always end up with error messages and functions not working right.
    Can someone please help me?

    I have a form, inside is 1 Text Field and 2 Password Fields. What I'm
    looking to do is:
    - Make sure password fields are equal
    - Set Minimum/Maximum amount of characters in each field
    - Disable submit button after clicking submit

    I plan to add more text fields in the future. Will I be able to? I
    know how to add it in the form, but how do I change the script?

    Any help is appreciated. What I have so far may not be worth a look,
    it's not even working, I don't know why, but here is goes:

    <html>
    <head>
    <title>Test Page 01</title>
    <script language="JavaS cript">
    <!--
    function checkPw(element ) {
    pw1 = document.FormNa me.pw1.value;
    pw2 = element.value;
    if (pw1 != pw2) {
    alert ("Passwords do not match. Please re-enter.");
    document.FormNa me.pw1.value="" ;
    document.FormNa me.pw2.value="" ;
    document.FormNa me.pw1.focus();
    return false;
    }
    }
    // -->
    </script>
    </head>
    <body>
    <form action="http://www.photoshelf. net/cgi-bin/mailto"
    onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
    name="FormName" >
    <input type="hidden" name="THANKURL"
    value="http://www.somepage.or g/ok.htm">
    <input type="hidden" name="RECIPIENT " value="someone@ somepage.com">
    <input type="text" name="UserName" size="20" maxlength="60">
    <input type="password" name="pw1" size="20" maxlength="60">
    <input type="password" name="pw2" size="20" maxlength="60"
    onBlur="checkPw (this)"><br>
    <input type="submit" value="Submit Application" onClick="if(thi s.value
    == 'Continue') this.form.submi t();">
    </form>
    </body>
    </html>

    Thanks. Mike
  • Lasse Reichstein Nielsen

    #2
    Re: password field validation - help

    msebben@photosh elf.net (Mike) writes:
    [color=blue]
    > I have a form, inside is 1 Text Field and 2 Password Fields. What I'm
    > looking to do is:
    > - Make sure password fields are equal
    > - Set Minimum/Maximum amount of characters in each field
    > - Disable submit button after clicking submit
    >
    > I plan to add more text fields in the future. Will I be able to?[/color]

    Sure.
    [color=blue]
    > I know how to add it in the form, but how do I change the script?[/color]

    It's modular. You add clauses for each thing you want to test.

    Remember the <!DOCTYPE ...> declaration. It's required in HTML
    [color=blue]
    > <html>
    > <head>
    > <title>Test Page 01</title>
    > <script language="JavaS cript">[/color]

    The type attribute is required in HTML 4, and is always sufficient:
    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    Not necessary.
    [color=blue]
    > function checkPw(element ) {[/color]

    You call this function in the second password field's onblur handler.
    Just wait until submission.

    You only need one function to validate. We call it from the onsubmit
    handler with the form as argument.
    ---
    function validate(form) {
    // check user anme between 6 and 10 characters:
    var user = form.elements[UserName];
    if (user.value.len gth < 6 || user.value.leng th > 10) {
    alert("User name must be between 6 and 10 characters!");
    user.focus();
    return false;
    }
    // passwords equal and between 6 and 8 characters
    var pwd1 = form.elements["pw1"];
    var pwd2 = form.elements["pw2"];
    if (pwd1.value != pwd2.value) {
    alert("Password s must be equal!");
    pwd1.focus();
    return false;
    }
    if (pwd1.length < 6 || pwd1.length > 8) {
    alert("Password s must be between 6 and 8 characters!");
    pwd1.focus();
    return false;
    }
    // form is ok, prevent further submit.
    var submit = form.elements["submitButt on"];
    submit.value = "Already submitted!";
    submit.disabled = true;
    return true;
    }
    ---
    [color=blue]
    > <form action="http://www.photoshelf. net/cgi-bin/mailto"
    > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
    > name="FormName" >[/color]

    change onsubmit to
    onsubmit="retur n validate(this); "
    or whatever function name you want.
    [color=blue]
    > <input type="submit" value="Submit Application" onClick="if(thi s.value
    > == 'Continue') this.form.submi t();">[/color]

    Give the submit button a name so we can find and disable it.

    Good luck
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Michael Winter

      #3
      Re: password field validation - help

      Mike wrote on 29 Nov 2003:
      [color=blue]
      > I've been trying for the past week to put a simple code
      > together. I have done a LOT of searching, found scripts showing
      > the functions I would like to use, however when I mix them it
      > all goes wrong, somehow I always end up with error messages and
      > functions not working right. Can someone please help me?
      >
      > I have a form, inside is 1 Text Field and 2 Password Fields.
      > What I'm looking to do is:
      > - Make sure password fields are equal
      > - Set Minimum/Maximum amount of characters in each field
      > - Disable submit button after clicking submit[/color]

      That's a dangerous thing to do. If a user clicks the 'Back' button,
      the submit button will still be disabled (control states are usually
      saved) and the user has to go back again, then forward to restore the
      initial settings. It might not matter in this case, though (due to
      the nature of the form).
      [color=blue]
      > I plan to add more text fields in the future. Will I be able to?
      > I know how to add it in the form, but how do I change the
      > script?[/color]

      The text fields have nothing to do with the validation of the
      password fields so you could add anything you like and it will have
      no effect, or does the number of characters condition you state above
      apply to literally /every/ field? If so, and the limits are the same
      for all fields, or all types of field (60 for password types, 30 for
      text, 1000 for textarea, etc), there is no problem. If not (the
      limits vary from field to field) it could become a little more
      complicated. You'll have to explain more about the changes you might
      make.
      [color=blue]
      > <script language="JavaS cript">[/color]

      There is no need to use the language attribute (it is, in fact,
      discouraged), but you /must/ use type (type="text/javascript")
      [color=blue]
      > <!--[/color]

      You don't need to use SGML comments in SCRIPT blocks.
      [color=blue]
      > function checkPw(element ) {
      > pw1 = document.FormNa me.pw1.value;[/color]

      You should use the forms and elements collections, like so:

      pw1 = document.forms['FormName'].elements['pw1'].value;

      It aids in compatibility. You should also use the var keyword to
      declare the variable, otherwise it ends up becoming a global.
      [color=blue]
      > pw2 = element.value;
      > if (pw1 != pw2) {
      > alert ("Passwords do not match. Please re-enter.");[/color]

      You should qualify that using window. That is, window.alert(.. .). The
      collections comment above applies to these, too:
      [color=blue]
      > document.FormNa me.pw1.value="" ;
      > document.FormNa me.pw2.value="" ;
      > document.FormNa me.pw1.focus();
      > return false;[/color]

      Returning false should not be necessary here (and may not do
      anything, anyway). The focus() call should keep the box in focus.
      [color=blue]
      > }[/color]

      Not that it matters here, due to what I just wrote, but in future, I
      would suggest that if you return a value for one code path, every
      code path should return a value.
      [color=blue]
      > }
      > // -->
      > </script>
      > </head>
      > <body>
      > <form action="http://www.photoshelf. net/cgi-bin/mailto"
      > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
      > name="FormName" >[/color]

      You're using intrinsic events, but you didn't (in this sample) set
      the default scripting language. This is invalid HTML. Place the
      following META element in the document HEAD:

      <META http-equiv="Content-Script-Type" content="text/javascript">
      [color=blue]
      > <input type="hidden" name="THANKURL"
      > value="http://www.somepage.or g/ok.htm">
      > <input type="hidden" name="RECIPIENT "
      > value="someone@ somepage.com">[/color]

      This is a /huge/ security risk. Never use a mail form that requires a
      full "To:" address - it's an easy target for spam (both for receiving
      it, and sending it).
      [color=blue]
      > <input type="text" name="UserName"
      > size="20" maxlength="60"> <input type="password" name="pw1"
      > size="20" maxlength="60"> <input type="password" name="pw2"
      > size="20" maxlength="60" onBlur="checkPw (this)"><br>[/color]

      The check should really be done upon submission. If I choose to do
      something else before completing a matching password (including
      changing my original password), I'll be trapped in this input field.
      There's also no need to pass the entire object: just pass the value
      using this.value.
      [color=blue]
      > <input type="submit" value="Submit Application"
      > onClick="if(thi s.value == 'Continue') this.form.submi t();">[/color]

      From what you've shown, that onclick event doesn't seem to do
      anything. It would have been helpful if you had shown the function
      called by the form's onsubmit event.

      Apply the suggestions I've made so far. If it's not enough, say so,
      and include the function called by the onsubmit event and explain
      your restrictions in more detail, and I'll try again.

      Good luck,
      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

      Comment

      • Mike

        #4
        Re: password field validation - help

        Michael Winter <M.Winter@bluey onder.co.uk.inv alid> wrote in message news:<Xns9442AC 31A84B5MWinterB lueyonder@193.3 8.113.46>...[color=blue]
        > Mike wrote on 29 Nov 2003:
        >[color=green]
        > > I've been trying for the past week to put a simple code
        > > together. I have done a LOT of searching, found scripts showing
        > > the functions I would like to use, however when I mix them it
        > > all goes wrong, somehow I always end up with error messages and
        > > functions not working right. Can someone please help me?
        > >
        > > I have a form, inside is 1 Text Field and 2 Password Fields.
        > > What I'm looking to do is:
        > > - Make sure password fields are equal
        > > - Set Minimum/Maximum amount of characters in each field
        > > - Disable submit button after clicking submit[/color]
        >
        > That's a dangerous thing to do. If a user clicks the 'Back' button,
        > the submit button will still be disabled (control states are usually
        > saved) and the user has to go back again, then forward to restore the
        > initial settings. It might not matter in this case, though (due to
        > the nature of the form).
        >[color=green]
        > > I plan to add more text fields in the future. Will I be able to?
        > > I know how to add it in the form, but how do I change the
        > > script?[/color]
        >
        > The text fields have nothing to do with the validation of the
        > password fields so you could add anything you like and it will have
        > no effect, or does the number of characters condition you state above
        > apply to literally /every/ field? If so, and the limits are the same
        > for all fields, or all types of field (60 for password types, 30 for
        > text, 1000 for textarea, etc), there is no problem. If not (the
        > limits vary from field to field) it could become a little more
        > complicated. You'll have to explain more about the changes you might
        > make.
        >[color=green]
        > > <script language="JavaS cript">[/color]
        >
        > There is no need to use the language attribute (it is, in fact,
        > discouraged), but you /must/ use type (type="text/javascript")
        >[color=green]
        > > <!--[/color]
        >
        > You don't need to use SGML comments in SCRIPT blocks.
        >[color=green]
        > > function checkPw(element ) {
        > > pw1 = document.FormNa me.pw1.value;[/color]
        >
        > You should use the forms and elements collections, like so:
        >
        > pw1 = document.forms['FormName'].elements['pw1'].value;
        >
        > It aids in compatibility. You should also use the var keyword to
        > declare the variable, otherwise it ends up becoming a global.
        >[color=green]
        > > pw2 = element.value;
        > > if (pw1 != pw2) {
        > > alert ("Passwords do not match. Please re-enter.");[/color]
        >
        > You should qualify that using window. That is, window.alert(.. .). The
        > collections comment above applies to these, too:
        >[color=green]
        > > document.FormNa me.pw1.value="" ;
        > > document.FormNa me.pw2.value="" ;
        > > document.FormNa me.pw1.focus();
        > > return false;[/color]
        >
        > Returning false should not be necessary here (and may not do
        > anything, anyway). The focus() call should keep the box in focus.
        >[color=green]
        > > }[/color]
        >
        > Not that it matters here, due to what I just wrote, but in future, I
        > would suggest that if you return a value for one code path, every
        > code path should return a value.
        >[color=green]
        > > }
        > > // -->
        > > </script>
        > > </head>
        > > <body>
        > > <form action="http://www.photoshelf. net/cgi-bin/mailto"
        > > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
        > > name="FormName" >[/color]
        >
        > You're using intrinsic events, but you didn't (in this sample) set
        > the default scripting language. This is invalid HTML. Place the
        > following META element in the document HEAD:
        >
        > <META http-equiv="Content-Script-Type" content="text/javascript">
        >[color=green]
        > > <input type="hidden" name="THANKURL"
        > > value="http://www.somepage.or g/ok.htm">
        > > <input type="hidden" name="RECIPIENT "
        > > value="someone@ somepage.com">[/color]
        >
        > This is a /huge/ security risk. Never use a mail form that requires a
        > full "To:" address - it's an easy target for spam (both for receiving
        > it, and sending it).
        >[color=green]
        > > <input type="text" name="UserName"
        > > size="20" maxlength="60"> <input type="password" name="pw1"
        > > size="20" maxlength="60"> <input type="password" name="pw2"
        > > size="20" maxlength="60" onBlur="checkPw (this)"><br>[/color]
        >
        > The check should really be done upon submission. If I choose to do
        > something else before completing a matching password (including
        > changing my original password), I'll be trapped in this input field.
        > There's also no need to pass the entire object: just pass the value
        > using this.value.
        >[color=green]
        > > <input type="submit" value="Submit Application"
        > > onClick="if(thi s.value == 'Continue') this.form.submi t();">[/color]
        >
        > From what you've shown, that onclick event doesn't seem to do
        > anything. It would have been helpful if you had shown the function
        > called by the form's onsubmit event.
        >
        > Apply the suggestions I've made so far. If it's not enough, say so,
        > and include the function called by the onsubmit event and explain
        > your restrictions in more detail, and I'll try again.
        >
        > Good luck,
        > Mike[/color]

        Hi Mike: Without typing anything on the fields and simply clicking on
        submit, IE6 errors:
        "Line 22, Char 1, Object Expected"

        I'm sure I screwed up!:

        <html>
        <head>
        <title>Test Page 01</title>
        <META http-equiv="Content-Script-Type" content="text/javascript">
        <script type="text/javascript">
        function checkPw(element ) {
        pw1 = document.forms['FormName'].elements['pw1'].value;

        pw2 = element.value;
        if (pw1 != pw2) {
        window.alert ("Passwords do not match. Please re-enter.");
        document.FormNa me.pw1.value="" ;
        window.alert ("Please enter a password.");
        document.FormNa me.pw2.value="" ;
        window.alert ("Please re-enter your password.");
        document.FormNa me.pw1.focus();
        }
        // -->
        </script>
        </head>
        <body>
        <form action="http://www.photoshelf. net/cgi-bin/mailto"
        onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
        name="FormName" >
        <input type="text" name="UserName" size="20" maxlength="60">
        <input type="password" name="pw1" size="20" maxlength="60">
        <input type="password" name="pw2" size="20" maxlength="60"
        "this.value"><b r>
        <input type="submit" value="Submit Application"
        onSubmit="if(th is.value == 'Continue') this.form.submi t();">
        </body>
        </html>

        Thanks in advance for all replies. Mike

        Comment

        • Michael Winter

          #5
          Re: password field validation - help

          Mike wrote on 30 Nov 2003:
          [color=blue]
          > <html>
          > <head>
          > <title>Test Page 01</title>
          > <META http-equiv="Content-Script-Type"
          > content="text/javascript"> <script type="text/javascript">
          > function checkPw(element ) {
          > pw1 = document.forms['FormName'].elements['pw1'].value;
          >
          > pw2 = element.value;
          > if (pw1 != pw2) {
          > window.alert ("Passwords do not match. Please
          > re-enter."); document.FormNa me.pw1.value="" ;
          > window.alert ("Please enter a password.");
          > document.FormNa me.pw2.value="" ;
          > window.alert ("Please re-enter your password.");
          > document.FormNa me.pw1.focus();
          > }
          > // -->
          > </script>
          > </head>
          > <body>
          > <form action="http://www.photoshelf. net/cgi-bin/mailto"
          > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
          > name="FormName" >
          > <input type="text" name="UserName" size="20" maxlength="60">
          > <input type="password" name="pw1" size="20" maxlength="60">
          > <input type="password" name="pw2" size="20" maxlength="60"
          > "this.value"><b r>
          > <input type="submit" value="Submit Application"
          > onSubmit="if(th is.value == 'Continue') this.form.submi t();">
          > </body>
          > </html>[/color]

          If this is exactly what you are testing, it's because the function
          (used in the form onsubmit event) FrontPage_Form1 _Validator() doesn't
          exist.

          You should remove the closing SGML comment (// -->) at the end of the
          script block.

          Finally, you should remove the onsubmit event on the submit button.
          INPUT elements (of any type) don't have such an event, so it means
          and does nothing.

          Here's some advice to help you do the other things you asked about in
          your original post:

          You've already got the maximum lengths restricted in your HTML. To
          set a minimum length, use [control reference].value.length. For
          example:

          document.forms['FormName'].elements['pw1'].value.length

          To disable the submit button, place the following at the end of the
          validation script so it will be executed when the form validates
          correctly:

          document.forms['FormName'].elements['submitButtonNa me'].disabled =
          true;

          If you make all these changes and add a proper onsubmit event handler
          for the form, your document above would look something like:

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">

          <HTML>
          <HEAD>
          <META http-equiv="Content-Script-Type" content="text/javascript">

          <TITLE>Test Page 01</TITLE>

          <SCRIPT type="text/javascript">
          function checkPw(form) {
          var pw1 = form.elements['pw1'];
          var pw2 = form.elements['pw2'];
          var minPasswordLeng th = 6;

          // Check password length
          if ((minPasswordLe ngth > pw1.value.lengt h)
          || (minPasswordLen gth > pw2.value.lengt h)) {
          window.alert(
          'Passwords must be at least ' + minPasswordLeng th
          + ' characters long.');
          pw1.focus();
          return false;
          }

          // Check password match
          if (pw1.value != pw2.value) {
          window.alert(
          'The passwords do not match. Please re-enter.');
          pw1.value = '';
          pw2.value = '';
          pw1.focus();
          return false;
          }

          // By now, the passwords have been validated. The form can be
          // submitted, and the submit button (I've named it 'apply'
          // for this example) can be disabled.
          form.elements['apply'].disabled = true;
          return true;
          }
          </SCRIPT>
          </HEAD>

          <BODY>
          <FORM name="FormName" method="post"
          action="http://www.photoshelf. net/cgi-bin/mailto"
          onsubmit="retur n checkPw(this)">
          <INPUT type="text" name="UserName" size="20" maxlength="60">
          <INPUT type="password" name="pw1" size="20" maxlength="60">
          <INPUT type="password" name="pw2" size="20" maxlength="60">
          <INPUT type="submit" name="apply" value="Submit Application">
          </BODY>
          </HTML>

          Hope this helps,
          Mike

          Sorry, I didn't mean to write this for you, but there were quite a
          few mistakes creeping in that would take too much explanation to fix.
          I think I was giving you too much new information without explaining
          it properly. My fault.

          --
          Michael Winter
          M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

          Comment

          • Mike

            #6
            Re: password field validation - help

            Michael Winter <M.Winter@bluey onder.co.uk.inv alid> wrote in message news:<Xns944496 C0A542DMWinterB lueyonder@193.3 8.113.46>...[color=blue]
            > Mike wrote on 30 Nov 2003:
            >[color=green]
            > > <html>
            > > <head>
            > > <title>Test Page 01</title>
            > > <META http-equiv="Content-Script-Type"
            > > content="text/javascript"> <script type="text/javascript">
            > > function checkPw(element ) {
            > > pw1 = document.forms['FormName'].elements['pw1'].value;
            > >
            > > pw2 = element.value;
            > > if (pw1 != pw2) {
            > > window.alert ("Passwords do not match. Please
            > > re-enter."); document.FormNa me.pw1.value="" ;
            > > window.alert ("Please enter a password.");
            > > document.FormNa me.pw2.value="" ;
            > > window.alert ("Please re-enter your password.");
            > > document.FormNa me.pw1.focus();
            > > }
            > > // -->
            > > </script>
            > > </head>
            > > <body>
            > > <form action="http://www.photoshelf. net/cgi-bin/mailto"
            > > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
            > > name="FormName" >
            > > <input type="text" name="UserName" size="20" maxlength="60">
            > > <input type="password" name="pw1" size="20" maxlength="60">
            > > <input type="password" name="pw2" size="20" maxlength="60"
            > > "this.value"><b r>
            > > <input type="submit" value="Submit Application"
            > > onSubmit="if(th is.value == 'Continue') this.form.submi t();">
            > > </body>
            > > </html>[/color]
            >
            > If this is exactly what you are testing, it's because the function
            > (used in the form onsubmit event) FrontPage_Form1 _Validator() doesn't
            > exist.
            >
            > You should remove the closing SGML comment (// -->) at the end of the
            > script block.
            >
            > Finally, you should remove the onsubmit event on the submit button.
            > INPUT elements (of any type) don't have such an event, so it means
            > and does nothing.
            >
            > Here's some advice to help you do the other things you asked about in
            > your original post:
            >
            > You've already got the maximum lengths restricted in your HTML. To
            > set a minimum length, use [control reference].value.length. For
            > example:
            >
            > document.forms['FormName'].elements['pw1'].value.length
            >
            > To disable the submit button, place the following at the end of the
            > validation script so it will be executed when the form validates
            > correctly:
            >
            > document.forms['FormName'].elements['submitButtonNa me'].disabled =
            > true;
            >
            > If you make all these changes and add a proper onsubmit event handler
            > for the form, your document above would look something like:
            >
            > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            > "http://www.w3.org/TR/html4/strict.dtd">
            >
            > <HTML>
            > <HEAD>
            > <META http-equiv="Content-Script-Type" content="text/javascript">
            >
            > <TITLE>Test Page 01</TITLE>
            >
            > <SCRIPT type="text/javascript">
            > function checkPw(form) {
            > var pw1 = form.elements['pw1'];
            > var pw2 = form.elements['pw2'];
            > var minPasswordLeng th = 6;
            >
            > // Check password length
            > if ((minPasswordLe ngth > pw1.value.lengt h)
            > || (minPasswordLen gth > pw2.value.lengt h)) {
            > window.alert(
            > 'Passwords must be at least ' + minPasswordLeng th
            > + ' characters long.');
            > pw1.focus();
            > return false;
            > }
            >
            > // Check password match
            > if (pw1.value != pw2.value) {
            > window.alert(
            > 'The passwords do not match. Please re-enter.');
            > pw1.value = '';
            > pw2.value = '';
            > pw1.focus();
            > return false;
            > }
            >
            > // By now, the passwords have been validated. The form can be
            > // submitted, and the submit button (I've named it 'apply'
            > // for this example) can be disabled.
            > form.elements['apply'].disabled = true;
            > return true;
            > }
            > </SCRIPT>
            > </HEAD>
            >
            > <BODY>
            > <FORM name="FormName" method="post"
            > action="http://www.photoshelf. net/cgi-bin/mailto"
            > onsubmit="retur n checkPw(this)">
            > <INPUT type="text" name="UserName" size="20" maxlength="60">
            > <INPUT type="password" name="pw1" size="20" maxlength="60">
            > <INPUT type="password" name="pw2" size="20" maxlength="60">
            > <INPUT type="submit" name="apply" value="Submit Application">
            > </BODY>
            > </HTML>
            >
            > Hope this helps,
            > Mike
            >
            > Sorry, I didn't mean to write this for you, but there were quite a
            > few mistakes creeping in that would take too much explanation to fix.
            > I think I was giving you too much new information without explaining
            > it properly. My fault.[/color]



            Excellent, worked beautiful, I can take it from here! Thanks a lot,
            you saved me a lot of time. Thanks.
            Mike

            Comment

            • Mike

              #7
              Re: password field validation - help

              msebben@photosh elf.net (Mike) wrote in message news:<5eee303f. 0312021816.a8fb 836@posting.goo gle.com>...[color=blue]
              > Michael Winter <M.Winter@bluey onder.co.uk.inv alid> wrote in message news:<Xns944496 C0A542DMWinterB lueyonder@193.3 8.113.46>...[color=green]
              > > Mike wrote on 30 Nov 2003:
              > >[color=darkred]
              > > > <html>
              > > > <head>
              > > > <title>Test Page 01</title>
              > > > <META http-equiv="Content-Script-Type"
              > > > content="text/javascript"> <script type="text/javascript">
              > > > function checkPw(element ) {
              > > > pw1 = document.forms['FormName'].elements['pw1'].value;
              > > >
              > > > pw2 = element.value;
              > > > if (pw1 != pw2) {
              > > > window.alert ("Passwords do not match. Please
              > > > re-enter."); document.FormNa me.pw1.value="" ;
              > > > window.alert ("Please enter a password.");
              > > > document.FormNa me.pw2.value="" ;
              > > > window.alert ("Please re-enter your password.");
              > > > document.FormNa me.pw1.focus();
              > > > }
              > > > // -->
              > > > </script>
              > > > </head>
              > > > <body>
              > > > <form action="http://www.photoshelf. net/cgi-bin/mailto"
              > > > onsubmit="retur n FrontPage_Form1 _Validator(this )" method="post"
              > > > name="FormName" >
              > > > <input type="text" name="UserName" size="20" maxlength="60">
              > > > <input type="password" name="pw1" size="20" maxlength="60">
              > > > <input type="password" name="pw2" size="20" maxlength="60"
              > > > "this.value"><b r>
              > > > <input type="submit" value="Submit Application"
              > > > onSubmit="if(th is.value == 'Continue') this.form.submi t();">
              > > > </body>
              > > > </html>[/color]
              > >
              > > If this is exactly what you are testing, it's because the function
              > > (used in the form onsubmit event) FrontPage_Form1 _Validator() doesn't
              > > exist.
              > >
              > > You should remove the closing SGML comment (// -->) at the end of the
              > > script block.
              > >
              > > Finally, you should remove the onsubmit event on the submit button.
              > > INPUT elements (of any type) don't have such an event, so it means
              > > and does nothing.
              > >
              > > Here's some advice to help you do the other things you asked about in
              > > your original post:
              > >
              > > You've already got the maximum lengths restricted in your HTML. To
              > > set a minimum length, use [control reference].value.length. For
              > > example:
              > >
              > > document.forms['FormName'].elements['pw1'].value.length
              > >
              > > To disable the submit button, place the following at the end of the
              > > validation script so it will be executed when the form validates
              > > correctly:
              > >
              > > document.forms['FormName'].elements['submitButtonNa me'].disabled =
              > > true;
              > >
              > > If you make all these changes and add a proper onsubmit event handler
              > > for the form, your document above would look something like:
              > >
              > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              > > "http://www.w3.org/TR/html4/strict.dtd">
              > >
              > > <HTML>
              > > <HEAD>
              > > <META http-equiv="Content-Script-Type" content="text/javascript">
              > >
              > > <TITLE>Test Page 01</TITLE>
              > >
              > > <SCRIPT type="text/javascript">
              > > function checkPw(form) {
              > > var pw1 = form.elements['pw1'];
              > > var pw2 = form.elements['pw2'];
              > > var minPasswordLeng th = 6;
              > >
              > > // Check password length
              > > if ((minPasswordLe ngth > pw1.value.lengt h)
              > > || (minPasswordLen gth > pw2.value.lengt h)) {
              > > window.alert(
              > > 'Passwords must be at least ' + minPasswordLeng th
              > > + ' characters long.');
              > > pw1.focus();
              > > return false;
              > > }
              > >
              > > // Check password match
              > > if (pw1.value != pw2.value) {
              > > window.alert(
              > > 'The passwords do not match. Please re-enter.');
              > > pw1.value = '';
              > > pw2.value = '';
              > > pw1.focus();
              > > return false;
              > > }
              > >
              > > // By now, the passwords have been validated. The form can be
              > > // submitted, and the submit button (I've named it 'apply'
              > > // for this example) can be disabled.
              > > form.elements['apply'].disabled = true;
              > > return true;
              > > }
              > > </SCRIPT>
              > > </HEAD>
              > >
              > > <BODY>
              > > <FORM name="FormName" method="post"
              > > action="http://www.photoshelf. net/cgi-bin/mailto"
              > > onsubmit="retur n checkPw(this)">
              > > <INPUT type="text" name="UserName" size="20" maxlength="60">
              > > <INPUT type="password" name="pw1" size="20" maxlength="60">
              > > <INPUT type="password" name="pw2" size="20" maxlength="60">
              > > <INPUT type="submit" name="apply" value="Submit Application">
              > > </BODY>
              > > </HTML>
              > >
              > > Hope this helps,
              > > Mike
              > >
              > > Sorry, I didn't mean to write this for you, but there were quite a
              > > few mistakes creeping in that would take too much explanation to fix.
              > > I think I was giving you too much new information without explaining
              > > it properly. My fault.[/color]
              >
              >
              >
              > Excellent, worked beautiful, I can take it from here! Thanks a lot,
              > you saved me a lot of time. Thanks.
              > Mike[/color]

              One last thing, to control minimum length on text field, I should add
              the line below to the script right?

              document.forms['FormName'].elements['UserName'].value.length

              Where in the script? Does location matter?
              Thanks again. Mike

              Comment

              • Michael Winter

                #8
                Re: password field validation - help

                Mike wrote on 06 Dec 2003:

                <snipped script>
                [color=blue]
                > One last thing, to control minimum length on text field, I
                > should add the line below to the script right?
                >
                > document.forms['FormName'].elements['UserName'].value.length
                >
                > Where in the script? Does location matter?[/color]

                The minimum length is already checked by the script I wrote:

                function checkPw(form) {
                var pw1 = form.elements['pw1'];
                var pw2 = form.elements['pw2'];
                var minPasswordLeng th = 6;

                // Check password length
                if ((minPasswordLe ngth > pw1.value.lengt h)
                || (minPasswordLen gth > pw2.value.lengt h)) {
                window.alert(
                'Passwords must be at least ' + minPasswordLeng th
                + ' characters long.');
                pw1.focus();
                return false;
                }

                // Omitted the rest of the script...

                If you want to change the minimum length, change the initial value of
                the minPasswordLeng th variable. If you want to impose minimum limits
                on other fields, copy that if statement block and alter it as needed.
                For example:

                // To check username length
                var username = form.elements['UserName'];
                var minUsernameLeng th = 4;

                if (minUsernameLen gth > username.value. length) {
                window.alert(
                'Your username must be at least ' + minUsernameLeng th
                + ' characters long.');
                username.focus( );
                return false;
                }

                Hope that helps,
                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                Comment

                Working...