1

I have a common validation function

function connect_and_update_destination() {
        var flag = true;
        // Validate IP
        if (!validate_ip_address()) {
            alert(protect.lang.enter_valid_ip);
            return;
        }

        if ($('#pjm_alias').val() === '') {
            $('#pjm_alias').focus();
            alert(protect.lang.enter_alias);
            return;
        }

        if ($('#pjm_auth_name').val() === '') {
            $('#pjm_auth_name').focus();
            alert(protect.lang.enter_auth_name);
            return;
        }

        if ($('#pjm_auth_password').val() === '') {
            $('#pjm_auth_password').focus();
            alert(protect.lang.enter_auth_pwd);
            return;
        }
        var ip = $('#pjm_ip1').val()+'.'+$('#pjm_ip2').val()+'.'+$('#pjm_ip3').val()+'.'+$('#pjm_ip4').val();
        return establish_connection(ip, $('#pjm_alias').val(), $('#pjm_auth_name').val(), $('#pjm_auth_password').val());
    }


After successful validation it always return establish_connection(), My problem is that am calling this connect_and_update_destination() like this,

function first_call(){
  connect_and_update_destination();
}
function second_call(){
  connect_and_update_destination();
}


When first call i need to return after successful validation establish_connection(), But when ever i called its using second function [second_call()] it should not return establish_connection() insted of return establish_connection() i need to only return . But how can i do this, Means i dont want to enter establish_connection() when ever i call using second_call().

Thanks in advance.

1
  • if u r calling on button then show ur button code Commented Jun 18, 2012 at 8:03

2 Answers 2

1

You could do something as simple as passing a variable.

function connect_and_update_destination(establishConnection) {

 ...

 if(!establishConnection)
   return;

 return establish_connection(ip, $('#pjm_alias').val(), $('#pjm_auth_name').val(), $('#pjm_auth_password').val());
}

function first_call(){
  connect_and_update_destination(true);
}
function second_call(){
  connect_and_update_destination(false);
}

For a more generic solution, you could pass a callback:

function connect_and_update_destination(callback) {

    ...

    if(typeof callback === 'function') {
       return callback(ip);
    }

    return;
}

function first_call() {
    var result = connect_and_update_destination(function(ip) {
       return establish_connection(ip, $('#pjm_alias').val(), $('#pjm_auth_name').val(), $('#pjm_auth_password').val());
    });
}

function second_call() {
    connect_and_update_destination();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could add a parameter to the connect_and_update_destination function:

function connect_and_update_destination(establishConnection) {
    var flag = true;
    // Validate IP
    if (!validate_ip_address()) {
        alert(protect.lang.enter_valid_ip);
        return;
    }

    if ($('#pjm_alias').val() === '') {
        $('#pjm_alias').focus();
        alert(protect.lang.enter_alias);
        return;
    }

    if ($('#pjm_auth_name').val() === '') {
        $('#pjm_auth_name').focus();
        alert(protect.lang.enter_auth_name);
        return;
    }

    if ($('#pjm_auth_password').val() === '') {
        $('#pjm_auth_password').focus();
        alert(protect.lang.enter_auth_pwd);
        return;
    }

    if (!establishConnection) {
        return;
    }

    var ip = $('#pjm_ip1').val()+'.'+$('#pjm_ip2').val()+'.'+$('#pjm_ip3').val()+'.'+$('#pjm_ip4').val();
    return establish_connection(ip, $('#pjm_alias').val(), $('#pjm_auth_name').val(), $('#pjm_auth_password').val());
}

and then:

function first_call() {
    connect_and_update_destination(true);
}
function second_call() {
    connect_and_update_destination(false);
}

2 Comments

The problem is not solved.Because in second call its not checking all the validation. when ever the second call initiate its just return from the validation function, its not validate ip address etc.
Where are you validating this ip address? Inside the establish_connection function? If so you will have to pass the establishConnection parameter to this function so that it could validate the ip but not do whatever you don't want it to do.

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.