Search code examples
javascriptiphoneformssubmission

Empty Form Submission Javascript


I'm trying to stop people from sending me blank emails through my contact us on my website. On my main website I have succeeded with the following javascript:

<script type="text/javascript">
function checkForm(f)
{
if (f.elements['cf_name'].value == "" && f.elements['cf_email'].value == "" &&          f.elements['cf_message'].value == "")
{
    alert("You have not filled in all of the required fields.");
    return false;
}
else
{
    f.submit();
    return false;
}
}
</script>

Although this works, when I implemented the same on my mobile website, people are still able to send blank emails.

The HTML code I used for the form check is:

<form action="contact.php" method="post" name="sform" onSubmit="return checkForm(this);">

So my question is how do i stop form submission if the fields are empty, on a mobile device (specifically iPhone)


Solution

  • Are you sure this isn't a logic error? Seems like this:

    if (f.elements['cf_name'].value == "" && f.elements['cf_email'].value == "" &&          f.elements['cf_message'].value == "")
    

    Should be:

    if (f.elements['cf_name'].value == "" || f.elements['cf_email'].value == "" ||          f.elements['cf_message'].value == "")