0

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
3
Views
4 Years
Discussion Span
Last Post by Octet
1

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;
    }

Edited by dcdruck: Typo

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.