3

My form is submitted by a link using JavaScript, but I am also trying to validate the from justing jQuery validate. The validation doesn't work when submitted by the link, but it does if I change the link to a submit button. What am I doing wrong?

My form:

<form id="findmatch" method="post" action="search">
    <div>
        <label class="formlabel">Match Type
            <input type="text" name="matchtype" id="matchtype" class="forminput" />
        </label>

        <label class="formlabel">Location (postcode)
            <input type="text" name="location" id="location" class="forminput" />
        </label>

        <label class="formlabel">Radius (miles)
            <input type="text" name="Radius" id="Radius" class="forminput" />
        </label>

        <label class="formlabel">Keywords
            <input type="text" onblur="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" onchange="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" name="keywords" id="keywords" class="forminput" />
        </label>

        <input id="lat" class="hidden" name="lat" type="text" value="" />
        <input id="lon" class="hidden" name="lon" type="text" value="" />

        <a href="javascript:document.getElementById('findmatch').submit();" onmouseover="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" class="submit">Search</a>                        
    </div>
</form>

And my jQuery is

<script type="text/javascript">
    $(document).ready(function () {
        $("#findmatch").validate({
            rules: {
                location: "required",
                Radius: {
                    required: true,
                    digits: true
                },
                keywords: "required"
            },
            messages: {
                location: "Please enter your postcode",
                Radius: {
                    required: "Please enter a radius",
                    digits: "Please only enter numbers"
                },
                keywords: "Please enter the keywords you wish to search for"
            }
        });
    });
</script>
2
  • jQuery.validate() has a submit handler that you can set to suit. Perhaps that might help. docs.jquery.com/Plugins/Validation/validate Commented May 5, 2010 at 13:54
  • Why are you using a link for your submit button anyway? You should use HTML elements as they are intended to be used. Commented May 5, 2010 at 13:58

1 Answer 1

1

DEMO: http://jsbin.com/urole/3

$(function () { 
  $('#submit').click(function(e) {
  e.preventDefault();
    $("#commentForm").submit(); 
  }); 
    $("#commentForm").validate();
});

submit

Sign up to request clarification or add additional context in comments.

1 Comment

How do you do this with Submit button? I tried with submit button, this doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.