Reputation: 63
I am trying to validate a very simple form with javascript using '.submit()' function. The first if statement (name is ok) but it doesn't go into the second one (varsta) and I get no error. If I take out the condition '!$.isNumeric($('#varsta').val())' the script runs fine. Why is that? Thank you
<form method="POST">
<div>
<label for="nume">Nume</label>
<input name="nume" id="nume" type="text">
</div>
<div>
<label for="varsta">Varsta</label>
<input name="varsta" id="varsta" type="text">
</div>
<div>
<input type="submit" value="Push">
</div>
</form>
<div id="result"></div>
<script type="text/javascript">
$( document ).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
if ($('#nume').val() != "") {
if ($('#varsta').val() != ""||!$.isNumeric($('#varsta').val())) {
$('#result').html($('#nume').val() + '<br />' + $('#varsta').val());
} else {$('#result').text('Indoduceti varsta valida');}
} else { $('#result').text('Indoduceti numele');}
})
})
</script>
Upvotes: 0
Views: 65
Reputation: 963
Your code should be like:
if ($('#varsta').val() != "" && !$.isNumeric($('#varsta').val())) {
...
} else {
...
}
Upvotes: 0
Reputation: 18873
Instead of $.isNumeric()
try isNaN()
as shown :
if ($('#varsta').val() != "" || isNaN($('#varsta').val()))
{......}
Upvotes: 1