Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re. Form Validator

    I have this form field checker (code as follows) and it tells people what "required fields" haven't been completed when they submit. You define the required fields in an array (as you'll see).

    In this particular form (two fields - text and password) I've defined both fields as required - however if you leave both fields empty and press submit it alerts and says that you only left the text field incomplete - it overlooks the password field.

    Can somebody tell me what I'm doing wrong? Cheers!

    +++++++++++++++++++++++++++

    <script language="JavaScript">
    <!--

    function formCheck(formobj){
    // Enter name of mandatory fields
    var fieldRequired = Array("UserName", "PassWord");
    // Enter field description to appear in the dialog box
    var fieldDescription = Array("Username", "Password");
    // dialog message
    var alertMsg = "Please complete the following fields:\n";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
    switch(obj.type){
    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "select-multiple":
    if (obj.selectedIndex == -1){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "text":
    case "textarea":
    if (obj.value == "" || obj.value == null){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    default:
    }
    if (obj.type == undefined){
    var blnchecked = false;
    for (var j = 0; j < obj.length; j++){
    if (obj[j].checked){
    blnchecked = true;
    }
    }
    if (!blnchecked){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    }
    }
    }

    if (alertMsg.length == l_Msg){
    return true;
    }else{
    alert(alertMsg);
    return false;
    }
    }
    // -->
    </script>


    <form name="formcheck" onsubmit="return formCheck(this);">
    First Name: <input type="text" name="UserName" size="25"><br>
    Last Name: <input type="password" name="PassWord" size="25"><br>
    <input type="submit" value="Submit Form">
    </form>

  • #2
    New Coder
    Join Date
    Jan 2004
    Location
    Boston, MA
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Maybe...

    What I've come across most often in programming is that when doing this type of thing, the scripts will often pitch an error on the first error condition that it comes across. Now bear in mind that I haven't looked through your code thoroughly, but here's a suggestion in case you haven't looked at it. Oh, and when I'm posting these possible fixes, I've been doing mostly VB lately, so I'm a little rusty with the JS equivalents. Please bear with me.

    Make a section of your javascript that sets a variable for each required form element to either true or false, like "field.password.Value IsValid = True". Then have another section of code use kind of a For... Each statement to pull together a kind of combined error message containing everything about all of the invalid form fields.

    Again, I'm sorry if this is hard to follow, but like I said, I've been doing nothing but Visual Basic for about 6 months now. I hope this helps some.

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to define the input type=password in your function.

    Try this:
    (changes highlighted in red)
    Code:
    <script language="JavaScript">
    <!--
    
    function formCheck(formobj){
    // Enter name of mandatory fields
    var fieldRequired = Array("UserName", "PassWord");
    // Enter field description to appear in the dialog box
    var fieldDescription = Array("Username", "Password");
    // dialog message
    var alertMsg = "Please complete the following fields:\n";
    
    var l_Msg = alertMsg.length;
    
    for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
    switch(obj.type){
    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "select-multiple":
    if (obj.selectedIndex == -1){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    case "text":
    case "password":
    case "textarea":
    if (obj.value == "" || obj.value == null){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
    default:
    }
    if (obj.type == undefined){
    var blnchecked = false;
    for (var j = 0; j < obj.length; j++){
    if (obj[j].checked){
    blnchecked = true;
    }
    }
    if (!blnchecked){
    alertMsg += " - " + fieldDescription[i] + "\n";
    }
    }
    }
    }
    
    if (alertMsg.length == l_Msg){
    return true;
    }else{
    alert(alertMsg);
    return false;
    }
    }
    // -->
    </script>
    
    
    <form name="formcheck" onsubmit="return formCheck(this);">
    First Name: <input type="text" name="UserName" size="25"><br>
    Last Name: <input type="password" name="PassWord" size="25"><br>
    <input type="submit" value="Submit Form">
    </form>
    .....Willy

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cheers

    worked a charm........ thanx


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •