0

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.

2
  • 1
    where do u call validate() ? Commented Nov 28, 2014 at 6:08
  • I call validate from an input tag in a onkeyup attribut like so: <input onkeyup="validate('street')" autocomplete="off" class="form-control" type="text" value="" name="street" id="street" /> Commented Nov 28, 2014 at 6:09

1 Answer 1

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
            }
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow I feel like an idiot, sorry I was misunderstanding because it told me that the function was undefined
No problem. If this post solved your issue, please mark it as a correct answer

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.