Reputation: 3298
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?
Upvotes: 0
Views: 1016
Reputation: 21005
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()
Upvotes: 1