0

I'm submitting a form on a btn click using jquery ajax call. I'm doing form validation and if input is not valid then need to stop the ajax call. I've tried the following code but form submits every time even when the conditions fail

<script type="text/javascript">
    $(document).ready(function () {

        $('#btnSubmit').click(function (evt) {
            if (ValidateUserInput()) {
                $.ajax({
                    type: 'GET',
                    url: '@Url.Action("SearchResults", "Request")',
                    data: { searchTerm: $("#searchTerm").val(), searchFilter: $("#searchFilter").val() },
                    success: function (data) {
                        $('#Results').html(data);
                    }
                });
            }

        });

    });

    function ValidateUserInput() {

        if ($('#searchFilter').val() == 'RequestId' && $('#searchTerm').val() != parseInt($('#searchTerm').val())) {
            $('#HiddenInvalidInputContainer').show(); 
            return false;

        }
        else {
            alert('testing');
            $('#HiddenInvalidInputContainer').hide(); 
            return true;
        }

    }

</script>

Any ideas?

1
  • You need to prevent default behaviour of submit button, otherwise the FORM is submited... Now use the search box here, there is many examples on how to prevent FORM submiting Commented Jan 25, 2015 at 12:24

1 Answer 1

1

You always get the else clause, as this is always false for a number

$('#searchTerm').val() != parseInt($('#searchTerm').val()

"1" != parseInt("1") is false, "1" !== parseInt("1") is true

if you are trying to test for not-a-number, then you could use isNaN()

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

Comments

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.