Results 1 to 5 of 5

Thread: [RESOLVED] Problem with validation function - not alerting

  1. #1
    Join Date
    Apr 2014

    resolved [RESOLVED] Problem with validation function - not alerting

    Hello all, I've got a new problem now. When I first build this form my validation form was working, but it started to get more and more complex and now it no longer alerts a message and stops submission. I can't figure out where the problem lies. Here's the code.

    PHP Code:
    //HTML - the form
    <form method="post" name="pizzaOrderForm" action="http://programs.siast.sk.ca/newmedia/onlinescripts/pizzaorder_olympic.php" target="_blank" onsubmit="return validateForm();">
    h3>Please enter your contact information:</h3>
    p><label for="FirstName_tf">First Name: </label><input name="FirstName_tf" id="FirstName_tf" type="text"></p>
    p><label for="LastName_tf">Last Name: </label><input name="LastName_tf" id="LastName_tf" type="text"></p>
    p><label for="Address_tf">Address: </label><input name="Address_tf" id="Address_tf" type="text"></p>
    p><label for="Phone_tf">Phone: </label><input name="Phone_tf" id="Phone_tf" type="text"></p>
    p><label for="Email_tf">Email: </label><input name="Email_tf" id="Email_tf" type="text"></p>
    h3>What size of pizza would you like to order?</h3>
    p><label onchange="calc()"><input type="radio" name="Size_rg" id="4" value="4" />Small 10" ($4.00)</label></p>
            <p><label onchange="
    calc()"><input type="radio" name="Size_rg" id="5" value="5" />Medium 12" ($5.00)</label></p>
    p><label onchange="calc()"><input type="radio" name="Size_rg" id="7" value="7" />Large 15" ($7.00)</label></p>
        <h3>What toppings would you like? All are $0.50 to add.</h3>
          <div id="
            <p><input id="
    Anchovies_cb" type="checkbox" value="0.50" onchange="calc()" /><label for="Anchovies_cb">Anchovies</label></p>
            <p><input id="
    DoubleCheese_cb" type="checkbox" value="0.50" onchange="calc()" /><label for="DoubleCheese_cb">Double Cheese</label></p>
            <p><input id="
    Pepperoni_cb" type="checkbox" value="0.50" onchange="calc()" /><label for="Pepperoni_cb">Pepperoni</label></p>
            <p><input id="
    Mushroom_cb" type="checkbox" value="0.50" onchange="calc()" /><label for="Mushroom_cb">Mushrooms0</label></p>
        <h3>Please select a payment method from the selection box.</h3>
             <select id="
    Payment_menu" size="4">
              <option value="
    select">- Select Payment Method -</option>
              <option value="
              <option value="
              <option value="
        <h3>Your order cost information:</h3>
        <p><label for="
    SubTotal_tb">Subtotal: $</label>
          <input type="
    text" name="SubTotal_tb" id="SubTotal_tb" value="" readOnly=true />
        <p><label for="
    PST_tb">PST: $</label>
          <input type="
    text" name="PST_tb" id="PST_tb" value="" readOnly=true />
        <p><label for="
    GST_tb">GST: $</label>
          <input type="
    text" name="GST_tb" id="GST_tb" value="" readOnly=true />
        <p><label for="
    Total_tb">TOTAL: $</label>
          <input type="
    text" name="Total_tb" id="Total_tb" value="" readOnly=true />
       <p><input type="
    submit" name="Submit_but"></p>

    //the Javascript
    function validateForm() {
        //assume that the form will have no errors. Set a variable to Boolean true.
    var okay = true; 
        //Declare and initialize a variable to hold a message for the user.
    var message = ""; 
        /*********** check the text fields ********************/
        //Start at the beginning of the form and check to see if the field is blank
        //that is to say, the value in each field is either null or an empty string ""
        if (document.getElementById("
    FirstName_tf").value == null || document.getElementById("FirstName_tf").value == "") {
            //The field is empty; set the Boolean variable to false
                    okay = false; 
            //add text to the user message that explains the error
            message += "
    Please fill in the First Name field.\n";
        //check the next field... 
        if (document.getElementById("
    LastName_tf").value == null || document.getElementById("LastName_tf").value == "") {
            //The field is empty; set the Boolean variable to false
            okay = false; 
            //add text to the user message that explains the error
            message += "
    Please fill in the Last Name field.\n";
        //check the next field... 
        if (document.getElementById("
    Address_tf").value == null || document.getElementById("Address_tf").value == "") {
            //The field is empty; set the Boolean variable to false
            okay = false; 
            //add text to the user message that explains the error
            message += "
    Please fill in the Address field.\n";
        //check the next field... 
        if (document.getElementById("
    Phone_tf").value == null || document.getElementById("Phone_tf").value == "") {
            //The field is empty; set the Boolean variable to false
            okay = false; 
            //add text to the user message that explains the error
            message += "
    Please fill in the Last Name field.\n";
        //check the next field... 
        var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        //Pattern taken from [url]http://www.w3resource.com/javascript/form/email-validation.php[/url] -- No author is given.
        if (!pattern.test(document.getElementById('Email_tf').value)) {
            //The field is empty; set the Boolean variable to false
            okay = false; 
            //add text to the user message that explains the error
            message += "
    Please enter a valid address into the E-Mail field.\n";
        /*********** check the radio inputs ********************/
        //assume that no button is selected. 
        var radioSelected = false; 
        //Start at the beginning of the radio buttons section and check to see if a radio input is checked
        if (document.getElementById("
    4").checked) {
            //the button is checked, set selected to true
            radioSelected = true; 
        //check the radio input...  
        if (document.getElementById("
    5").checked) {
            //the button is checked, set selected to true
            radioSelected = true;
        //check the radio input...  
        if (document.getElementById("
    7").checked) {
            //the button is checked, set selected to true
            radioSelected = true;
        //If after checking all the buttons, selected is still false, there is a problem. 
        if (!radioSelected) {
            //Set okay to false
            okay = false; 
            //add to the message 
            message += "
    Please select the size of pizza.\n"; 
        /*********** check the payment selection ********************/
        //In a select element, you know what has been selected by getting the selectedIndex
        //In this select, if the user has not picked an age, the selected index will be zero
        //That is because we have put a prompting message --Please select age group -- at index 0 of the select. 
        //assume everything is okay; set okay to true. 
        var okay = true;
    Payment_menu").selectedIndex == 0) {
            //set okay to false
            okay = false; 
            //add to the message
            message += "
    Please select a payment method.\n";
        //If the Boolean is no longer true, there were problems with the form. Alert the message. Otherwise, don't do anything. 
        if (!okay) {
        //return the Boolean.
        return okay;  

    Does anyone know what this won't alert the message instead of submitting?

  2. #2
    Join Date
    Jul 2011
    1. You have such code in the end of your function:
        //In a select element, you know what has been selected by getting the selectedIndex 
        //In this select, if the user has not picked an age, the selected index will be zero 
        //That is because we have put a prompting message --Please select age group -- at index 0 of the select.  
        //assume everything is okay; set okay to true.  
        var okay = true;
    I don't think that it is correct, because it overrides all previous validation.

    2. The next line is:
    if(document.getElementById("Payment_menu").selectedIndex == 0) {
    selectedIndex will be equal to 0 if the first element from the list is selected. If nothing selected - selectedIndex will be -1.

    These two things break your validation.

  3. #3
    Join Date
    Apr 2014
    Thanks for the assistance. I understand the first bit of code, don't reset the var okay, but I don't understand the second part of code you pointed out. I don't get what's wrong with it or what I'm supposed to do about it. A little extra help, please?

  4. #4
    Join Date
    Mar 2005
    Behind you...
    kromol was noting that when you try to validate your payment method select input, you are checking if the selectedIndex is equal to 0, and if so you invalidate the form and set an error message. But the selectedIndex of 0 is not the default value, -1 is. Thus you should be checking if the selectedIndex is equal to -1 rather than 0.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  5. #5
    Join Date
    Apr 2014
    Okay, that helped a bit more. I've make the corrections to my form and it seems to be working (in regards to the question here), so thank you both!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.39190 seconds
  • Memory Usage 2,936KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates