Results 1 to 7 of 7

Thread: form validation check before the page is submitted to asp

  1. #1
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default form validation check before the page is submitted to asp

    1) Script Title: Required Field Validation

    2) Script URL (on DD):
    <script language="JavaScript">
    <!--

    /***********************************************
    * Required field(s) validation v1.10- By NavSurf
    * Visit Nav Surf at http://navsurf.com
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/

    function formCheck(formobj){
    // Enter name of mandatory fields
    var fieldRequired = Array("name", "email", "employer", "address", "citystatezip", "phone");
    // Enter field description to appear in the dialog box
    var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip," "Phone");
    // 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>

    3) Describe problem: I am using this script however it doesn't validate the form first before going to the asp mail form

    which is here: <form action="http://www.voicesforhousing.org/authorize.asp" method="post" name="FormMail" onsubmit="return formCheck(formobj);" id="FormMail">

    How can I get the form to validate before it goes to the authorize.asp file or is there a way to put this information into the .asp file?

    Thank you for your help.

  2. #2
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Warning: Please include a link to the DD script in question in your post. See this thread for the proper posting format when asking a question. Also, format your code using the CODE tag please for easier reading.

    Are you saying you're having trouble getting the script to work like in the demo, that is to say, stop the form submission when one of the required fields isn't filled out? Based on the above the script seems set up correctly; please post a link to the page on your site that contains the problematic script so we can check it out.

  3. #3
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry for not including the information correctly, this is my first time posting.

    Here is the page with the form set up: http://www.coastalhousing.org/letter...orization.html

    The form works, however it doesn't validate the required fields.

  4. #4
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    There is at least one error as far as I can see in the way you've set up the script. The following line:

    Code:
    var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip," "Phone");
    The comma in red should appear on the outside of the text "City State Zip", so:

    Code:
    var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip", "Phone");

  5. #5
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I fixed the comma, however, the form is still not validating the required fields in the Javascript before it is being sent to the .asp file. Any other suggestions?
    http://www.coastalhousing.org/letter...orization.html

  6. #6
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Ah yes, there is another error, in your HTML:

    Code:
    onsubmit="return formCheck(formobj);"
    "formobj" should be changed to this instead:

    Code:
    onsubmit="return formCheck(this);"

  7. #7
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are a genius! Thank you for all your help!

Bookmarks

Posting Permissions

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