1

I have this html;

<form action="" method="post" name="ApplyForm" id="ApplyForm" enctype="multipart/form-data">
  <input type=text class="required"/>
  <input type="submit" onclick="return validateForm()" class="submit" value="Submit Application" title="Submit application" />

and my script;

    $(function () {
        $("#ApplyForm").validate();
    });

    function validateForm() {
        alert($(".email").valid());

        return false;
    }

So what's happening is if I alert out alert($("#ApplyForm").valid()); I always get true yet if I alert out the individual fields I get the expected results but the form still posts back;

this code works but I am unsure why the validate is not working on the form;

    function validateForm() {
        if ($(".required").valid() == 0)
            return false;
        else
            return true;
    }

I know this isn't much to go on but I'm hoping someone else has had this experiance and can tell me a solution.

griegs
  • 22,624
  • 33
  • 128
  • 205

3 Answers3

0

Try adding a name attribute to your first validated element within the form:

<input type=text required name='blah' />

I spent 2 hours tracking down this same issue, and I found that if the FIRST validated element in the form has a name attribute then everything works as you would expect (that is, .valid() will return false if the form is invalid). It seems to make no difference whether the other validated elements have name attributes or not.

In normal forms, you definitely need name attributes because that's what's used when the data gets submitted to the server. But in more modern environments, such as those using Knockout, there's no reason to have a name attribute on input elements, because the data-binding works to keep your data model updated.

Michael Bray
  • 14,998
  • 7
  • 42
  • 68
0

You should add

<input type="text" data-val="true" name="name" data-required="1">

For unobtrusive HTML 5-compatible attributes describe the validators to be attached to the input fields

Additional info Click here

Ganesh Pravin
  • 79
  • 1
  • 13
0
<form action="" method="post" name="ApplyForm" id="ApplyForm" onSubmit="return validateForm()" enctype="multipart/form-data">
    <input type=text class="required"/>
    <input type="submit" class="submit" value="Submit Application" title="Submit application" />
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
  • Sorry but i think you have missed the point. the call to validate the
    fails and always returns true which is incorrect
    – griegs Sep 27 '11 at 02:50