+ Post New Thread
Results 1 to 9 of 9
Web Development Thread, Problem with Form Validation in Coding and Web Development; I'm struggling to get my javascript form validation to work. I have read through a few tutorials and from what ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,818
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350

    Problem with Form Validation

    I'm struggling to get my javascript form validation to work. I have read through a few tutorials and from what i can see i think i have it right. Although it still doesn't work!

    Code:
    function validate_form ( )
    {
        valid = true;
     
        if ( contact_form.staffname.value == " " )
        {
            alert ( "Please fill in the 'Name' box." );
                                    contact_form.staffname.focus();
            valid = false;
        }
     
        if ( contact_form.email.value == " " )
        {
            alert ( "Please fill in the 'Your Email' box." );
                                    contact_form.email.focus();
            valid = false;
        }
     
    if ( contact_form.week[0].checked == false )
            {
                    alert ( "Please choose either Week A or B" );
                                                                    contact_form.week.focus();
                    valid = false;
            }
    if ( contact_form.absentallday[0].checked == false )
            {
                    alert ( "Please choose whether you are out all day or not." );
                                                                    contact_form.absentallday.focus();
                    valid = false;
            }
    if ( contact_form.reason[0].checked == false ) && ( contact_form.reason[1].checked == false ) && ( contact_form.reason[2].checked == false ) && ( contact_form.reason[3].checked == false ) && ( contact_form.reason[4].checked == false ) && ( contact_form.reason[5].checked == false ) && ( contact_form.reason[6].checked == false ) )
            {
                    alert ( "Please Select a reason for absence." );
                    contact_form.reason.focus();
                    valid = false;
            }     
                                    return valid;
                   
    }
    here is my form tag
    HTML Code:
    <form action="submitcover.php" method="post" name="contact_form" id="contact_form" onsubmit="return validate_form ( );">

    It doesn't work at all! it just submits the page regardless!

    i'm not quite sure what i have done wrong!

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,896
    Thank Post
    3,418
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Only thing I can see is the amount of brackets in your comparitive if statement at the end

    The IF Statement

    Half way down the above link they have more then one set of brackets around each comparitive section ie

    Code:
    if ((red == blue) && (red == green))             {
                   match = 'equal';
                } else {
                   match = 'unequal';
                }

  3. #3

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,818
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    okay i've added the extra brackets in (the example i had didn't show them!)

    it prompts for validation but still sends the email without letting you correct!?

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,896
    Thank Post
    3,418
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    does the return valid; code set valid to true in which case it thinks that everything is valid and it then sends the email

    using your original code without the correct amount of brackets

    Code:
    if ( contact_form.reason[0].checked == false ) && ( contact_form.reason[1].checked == false ) && ( contact_form.reason[2].checked == false ) && ( contact_form.reason[3].checked == false ) && ( contact_form.reason[4].checked == false ) && ( contact_form.reason[5].checked == false ) && ( contact_form.reason[6].checked == false ) )
            {
                    alert ( "Please Select a reason for absence." );
                    contact_form.reason.focus();
                    valid = false;
            }     
                                    return valid;
                   
    }
    In the if statement you set valid to false but afterwards it is set to return valid;

    Just curious if you comment out the return valid;

    Maybe its just me but I would of made it set the valid flag to true when the data entered is correct and then check on the last if statement to ensure all of them are valid ie true before sending the email

    Maybe Im just thinking about this in a backwards way
    Last edited by mac_shinobi; 22nd January 2011 at 11:35 AM.

  5. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,818
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    but shouldn't return valid just return the value of valid?

    its set to true at the top and if they statements are correct it sets it to false?

    I will give it a try tho

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,896
    Thank Post
    3,418
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Setting the valid flag to false to start with and setting them to true if the data requirement for said field is correct

    fair enough ref the return valid

    is there any code / function to send the email as I can't see which bit(s) of code are calling to send the email

    Am wondering if its the php code as when you submit its executing the php code

  7. #7

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,818
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    The PHP script just submits but I presumed if the valid flag was false then it shouldn't action!

  8. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,896
    Thank Post
    3,418
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Depends on what's submitting the email ie php or JavaScript and also if the code is checking the valid flag to see if each of thm is set to true before sending the data from the said form and if there is any invalid data ie any of the flags are false to not do anything

  9. Thanks to mac_shinobi from:

    glennda (22nd January 2011)

  10. #9

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,818
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    I have changed it so that if the javascript fails then it doesn't send the form.

    I put in document.formname.submit()
    return;
    }

    now it works

    Cheers for that prompted me not to give up trying!

SHARE:
+ Post New Thread

Similar Threads

  1. HTML/PHP form problem
    By glennda in forum Web Development
    Replies: 4
    Last Post: 17th November 2010, 10:22 AM
  2. VBA Code to copy all form updates to one form
    By acrobson in forum Coding
    Replies: 2
    Last Post: 27th January 2010, 07:04 PM
  3. Access form problem
    By Hecate in forum Office Software
    Replies: 4
    Last Post: 15th December 2009, 09:35 AM
  4. PHP form validation
    By thesk8rjesus in forum Web Development
    Replies: 10
    Last Post: 21st January 2009, 12:09 AM
  5. windows validation
    By strawberry in forum Windows
    Replies: 2
    Last Post: 7th May 2008, 05:24 PM

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
  •