0

I'm trying to call a function inside a form validation function, but it's not working. The function confirm_submit should be called if valid is true.

$(document).ready(function() {

$("#dialog").dialog({
  autoOpen: false, 
  buttons : {
   "Confirm" : function() {
    $('#form').submit();
        },
    "Cancel" : function() {
     $(this).dialog("close");
  }
 }
});


function confirm_submit(){
      $("#dialog").dialog('open');
}

});


function validate(){
valid = true;

if ( document.form.number.value == ""){
    alert ( "You need to complete the Number field." );
    valid = false;
    return valid;
}

if(valid){
confirm_submit();
}
}
4
  • 1
    Could you describe how it doesn't work? That's much more informative than just saying "it doesn't work". Commented Nov 19, 2013 at 2:24
  • 1
    The function confirm_submit is not being called. If I have $("#dialog").dialog('open'); outside of confirm_submit function, the dialog opens OK. Commented Nov 19, 2013 at 2:27
  • I have also tried return confirm_submit(); leaving out the if valid condition.. Commented Nov 19, 2013 at 2:29
  • Note that if I replace the if(valid){...} etc with a normal confirmation alert e.g. return confirm('Confirm submit'); that works OK Commented Nov 19, 2013 at 2:31

1 Answer 1

1

It's because your definition for confirm_submit is scoped inside your document.ready event. Move the definition after }); right above function validate(){.

This is basically what you're doing:

function x() {

    function y() {
        console.log('y')
    }   

    console.log('x');
    y();
}

function z() {
    console.log('z');
    y(); // throws: ReferenceError: y is not defined
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that works, now the dialog opens OK, but the forms just submits....I guess that's another problem for now.

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.