We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,355 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Skipping JavaScript and Going Straight to PHP - Form Validation

I am trying to validate a form for my website, at the moment the problem is it shall skip over the JavaScript bit entirely and go straight to the PHP side of it.

This is my HTML Form:

<form name = "Contact" action = "/PHP/UserActions/Contact.php" onsubmit = "return validateForm()" method = "POST" >

    <p> Contact Reason: </p> <br>

    <select name = "ContactReason" >

        <option value = "Null"                               >                                       </option>
        <option value = "Technical"                      > Technical/Bug             </option>
        <option value = "Legal"                              > Legal                             </option>
        <option value = "Suggestion"                 > Suggestion/Opinion    </option>
        <option value = "Other"                              > Other                             </option>

    </select> <br><br>

    <p> Title: </p> <br>

    <input type = "text" name = "Title" maxlength = "50" /> <br><br>

    <p> Message: </p> <br>

    <textarea name = "Message" maxlength = "1000" cols = "50" rows = "5" ></textarea> <br><br>

    <input type = "text" name = "BotDetect" maxlength = "1" class = "hidden" />

    <p> What String do you See? &nbsp; &nbsp; &nbsp; <img src = "/PHP/Services/Captcha.php" /> </p> <br><br>

    <input type = "text" name = "Captcha" maxlength = "4" /><br><br>

                <input type = "hidden" name = "Agree" value = "False" />

        <p> <input type = "checkbox" name = "Agree" value = "True" /> - I Understand and Agree to the Disclaimer and Privacy Policy </p> <br><br>

    <input type = "submit" value = " Send Message " />

</form>

And then this is my JavaScript section:

function validateForm()
{

    var ContactReason       = document.forms['Contact']['ContactReason'].value;
    var Title                           = document.forms['Contact']['Title'].value;
    var Message                     = document.forms['Contact']['Message'].value;
    var Captcha                     = document.forms['Contact']['Captcha'].value;

    if (ContactReason == Null)
    {
        alert("Please Enter a Contact Reason");
        return false;
    }

    if (Title == "" || Title == " " || Title == null)
    {
        alert("Please Enter a Title (Must Be Between 5 and 50 Characters)");
        return false;
    }

    if (Title.length < 5 || Title.length > 50)
    {
        alert("Please Enter a Title Between 5 and 50 Characters");
        return false;
    }

    if (Message == "" || Message == " " || Message == null)
    {
        alert("Please Enter a Message (Must Be Between 25 and 1000 Characters)");
        return false;
    }

    if (Message.length < 25 || Message.length > 1000)
    {
        alert("Please Enter a Message Between 25 and 1000 Characters");
        return false;
    }

    if (Captcha == "" || Captcha == " " || Captcha == null)
    {
        alert("Please Enter the Security Captcha (4 Characters Long)");
        return false;
    }

    if (Captcha.length < 4 || Captcha.length > 4)
    {
        alert("The Captcha is 4 Characters Long");
        return false;
    }

}

As I say, it just skips the JS bit and goes straight to the PHP.
It does go through the JS because if I add if (1 == 1) { alert("True"); return false; } it shall display 'True'.

Any help would be appreciated, thank you.

2
Contributors
2
Replies
25 Minutes
Discussion Span
6 Months Ago
Last Updated
3
Views
Question
Answered
AHarrisGsy
Posting Pro in Training
494 posts since Nov 2011
Reputation Points: 52
Solved Threads: 41
Skill Endorsements: 13

Line #9 in your JavaScript needs quotes around Null because you want to compare it to the string 'Null'. I think that should fix your issue.

    if (ContactReason == "Null")
    {
        alert("Please Enter a Contact Reason");
        return false;
    }
dcdruck
Junior Poster in Training
89 posts since Jul 2009
Reputation Points: 21
Solved Threads: 20
Skill Endorsements: 0

Aaah, works perfectly! I knew it was something small, thanks for the help!

AHarrisGsy
Posting Pro in Training
494 posts since Nov 2011
Reputation Points: 52
Solved Threads: 41
Skill Endorsements: 13
Question Answered as of 6 Months Ago by dcdruck

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0769 seconds using 2.71MB