I am relatively new at Javascript, and I have a page that calls a javascript function on onkeyup, but when called it throws an exception, Uncaught ReferenceError: validate is not defined.
<script type="text/javascript">
function validate(id){
alert(id);
$.ajax({
url: "http://example.com/restaurant/submit/verify",
type: "POST",
dataType: 'json',
data: {
field: id,
content: $('#' + id).val()
}
success: function(data){
alert(data);
if(data.status == "PASSED"){
$('#' + id).removeClass('has-error');
$('#' + id).addClass('has-success');
} else if(data.status == "FAILED") {
$('#' + id).removeClass('has-success');
$('#' + id).addClass('has-error');
$('#' + id + ' div').append('<span class="help-inline">' + data.error + '</span>')
}
}
});
}
</script>
Any help is greatly appreciated, if you need more info, don't hesitate to ask. Thanks for any and all help.
asked Nov 28, 2014 at 6:05
Ian Woodfill
1641 gold badge1 silver badge9 bronze badges
1 Answer 1
You forgot a comma and a semicolon:
function validate(id){
alert(id);
$.ajax({
url: "http://example.com/restaurant/submit/verify",
type: "POST",
dataType: 'json',
data: {
field: id,
content: $('#' + id).val()
}, // Here
success: function(data){
alert(data);
if(data.status == "PASSED"){
$('#' + id).removeClass('has-error');
$('#' + id).addClass('has-success');
} else if(data.status == "FAILED") {
$('#' + id).removeClass('has-success');
$('#' + id).addClass('has-error');
$('#' + id + ' div').append('<span class="help-inline">' + data.error + '</span>'); // and here
}
}
});
}
answered Nov 28, 2014 at 6:12
Salil Dabholkar
6223 silver badges7 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Ian Woodfill
Wow I feel like an idiot, sorry I was misunderstanding because it told me that the function was undefined
Salil Dabholkar
No problem. If this post solved your issue, please mark it as a correct answer
lang-js
validate()?onkeyupattribut like so:<input onkeyup="validate('street')" autocomplete="off" class="form-control" type="text" value="" name="street" id="street" />