1

I want to pass one parameter to a function called ForPaste().

My function is given below:

 var regex = /^[A-Za-z0-9ĀĒĪŌŪāēīōū\.\-\~\`\'' ]*$/;
    var SalaryRegex = /^[A-Za-z0-9\,\.\/\$ ]*$/;
        $.fn.ForPaste = function () {
            return this.each(function () {
                $(this).bind('input propertychange', function () {
                    var value = $(this).val();
                    if (!regex.test(value)) {
                        $(this).val("");
                    }
                });
            });
        };

This function is in a common JS file. It is called on individual pages. I want to test the regex depending on the parameter passed. So can I know the method about to call the ForPaste() function with the parameter.e.g $("#Text1").Forpaste('FromForm1'); and I get this FromForm1 in the ForPaste() function.

1
  • K T - why have you accepted only 4 answers for you 16 questions? And why do you ignore any comments? Commented Oct 1, 2013 at 6:24

2 Answers 2

1

You define a formal parameter for your function.

      // formal parameter--v
$.fn.ForPaste = function (the_value) {

    alert( the_value ); // displays the argument passed

    // rest of your code
};

Whatever value was passed to ForPaste() will be referenced by the_value. (Of course you can change the name to any valid identifier.)

Sign up to request clarification or add additional context in comments.

Comments

0

not sure what you are trying to do, because the answer seems so obvious. Will this do?

$.fn.ForPaste = function (theName) {
    if (theName.test(//)){
        return this.each(function () {
            ....
        });
    }
    return false
};

you can access the parameters by looking at arguments which isn't an array, but seems to be one. from within the function you can do an var theName=arguments[0] to get the value of the first parameter

Comments

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.