0

I am having trouble with my simple password validations.

These are the things that validation does

  • check if password is less than 8 characters
  • check if it is more than 15 characters
  • check if new password and retyped are mismatched

The form submits when two passwords are matched even if less than or greater than the validations

Please see my code:

function on_submit(dest) {

    var objForm = document.LogonForm;
    var strMissingInfo = "";

    if (dest == 'logon') {

        if (objForm.current.value != "") {

            if (objForm.new1.value.length < 8 || objForm.new1.value.length > 15) {
                strMissingInfo += "\n   Password must be 8 to 15 characters long";

            } else if (objForm.retype.value != objForm.new1.value) {
                strMissingInfo += "\n   Password mismatch!";

            }

            if (strMissingInfo != "") {
                alert(strMissingInfo);

            } else {
                alert(strMissingInfo);
                objForm.action.value = dest;
                objForm.submit();
            }
        }
    }
}

--- HTML Part

<input type="password" id="current" name="current"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="1" required/>

<input type="password" id="new1" name="new1"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="2" required />

<input type="password" id="retype" name="retype"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="3" required />

<a href="javascript:;">
                    <input id="logonButton" class="submit"
                        type="submit" name="Submit" value="Confirm" tabindex="4"
                        onclick="on_submit('logon')" />
                    </a>
MSSF-ONE Test
  • 11
  • 1
  • 5

3 Answers3

0

Since you don't mention the alert occuring, my guess is that you are calling this function with a button from within the form. That will automatically submit the form, regardless of the onclick function, unless you set the type on the button as described in this question: How to prevent buttons from submitting forms

<input id="logonButton" class="submit"
                    type="button" name="Submit" value="Confirm" tabindex="3"
                    onclick="on_submit('logon')" />
Community
  • 1
  • 1
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • Yes I am calling the function onClick of a submit button. Thanks,, – MSSF-ONE Test May 20 '13 at 03:24
  • ok then you're going to need to add type=button to the markup for that button. Otherwise it will submit the form automatically. Your function may or may not work, tough to tell for sure since I don't know what all your variables, are, but its not even running right now :) – Ben McCormick May 20 '13 at 03:26
  • it won't submit even if the validations are satisfied. – MSSF-ONE Test May 20 '13 at 03:28
  • uh huh, well are you sure its hitting the submit statement? Does it do the alert or does it throw an error? Like I said, I think your function may have other issues, but this was at least the first one. – Ben McCormick May 20 '13 at 03:29
  • it do the alert. I'll check my function again. Thanks :) – MSSF-ONE Test May 20 '13 at 03:32
0

if you want the form to fail on submission try returning false when validation fails.

        if (strMissingInfo != "") {
            alert(strMissingInfo);
            return false;
        }
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
0

You need to return either true (submit the form) or false (stop submission) as necessary from the function, and also use onclick="return on_submit('logon');" - or even better you should remove it and put onsubmit="return on_submit('logon');" on the form itself.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592