Matthew
Matthew

Reputation: 4607

Open a new window/tab

I am working on a donations website. In my page, I have a textbox which accepts a numeric value from the user (that is, money to be donated).

In my code-behind, I have a method which checks whether the value in the textbox is numeric. The method generates an error message if the number is invalid.

I also have a JavaScript which, after checking that the value in the textbox is numeric, opens a new tab to the website confirmation page, thanking the user for his donation. Here is the code of the javascript:

<script type="text/javascript">
        function Open_Window()
        {
            var textbox = document.getElementById('DonationTextBox');

            if (textbox.value != "")
            {
                if (isNan(textbox) == false)
                {
                    window.open("DonationConfirmation.aspx")
                }
            }
        }
    </script>

The problem is that the tab is NEVER opened, even if the number is valid. Can you please help me solve this problem? Thank you.

P.S.

Here is the code of the button that initiates the validation:

<asp:ImageButton ID="PayPalButton2" runat="server" ImageAlign="Middle" 
                                        ImageUrl="Resources/Icons/PayPalCheckOut.gif" 
                                        onclick="PayPalButton2_Click" OnClientClick="Open_Window()"/>

Upvotes: 0

Views: 582

Answers (5)

Sornakumar
Sornakumar

Reputation: 432

The function name is isNaN. Note: The final 'N' is capital. That should solve your problem.

Upvotes: 3

P. Galbraith
P. Galbraith

Reputation: 2527

Here is a stripped down working jsFiddle example:

http://jsfiddle.net/pjgalbraith/QZeSF/

The html:

<a href="javascript:void(0)" id="PayPalButton2">Open</a>
<textarea id="donationTextBox">1</textarea>​

And the js:

function openWindow() {
    if($('#donationTextBox').val() && isNaN($('#donationTextBox').val()) === false)
        window.open("http://www.google.com/", "mywindow");
}

$(document).ready(function() {
    $('#PayPalButton2').click(function(){
        openWindow();
    });
});

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1073968

First, I would recommend explicitly parsing the number, not relying on the implicit ToNumber operation that will be applied when you pass a string into isNaN. Presumably your users are inputting decimal, so if it's meant to be a whole number (e.g., 10), use:

var num = parseInt(textbox.value, 10);

If it's meant to be a number with a fractional component (e.g., 10.5), use:

var num = parseFloat(textbox.value);

You probably want parseFloat for a currency value.

Then your if condition becomes isNaN (note that the final N is capped) on num:

<script type="text/javascript">
        function Open_Window()
        {
            var textbox = document.getElementById('DonationTextBox');
            var num = parseInt(textbox.value, 10);

            if (!isNaN(num))
            {
                window.open("DonationConfirmation.aspx")
            }
        }
</script>

And lastly, are you sure that the client-side ID of the textbox really is 'DonationTextBox'? ASP auto-generates client-side IDs, you may need to use ClientID instead, e.g.:

var textbox = document.getElementById('<%=DonationTextBox.ClientID%>');

Upvotes: 1

Adrian Iftode
Adrian Iftode

Reputation: 15663

<script type="text/javascript">
    function Open_Window()
    {
        var textbox = document.getElementById('<%=DonationTextBox.ClientID%>');                        
        if (textbox.value != "" && !isNaN(textbox.value)) {                
             window.open("DonationConfirmation.aspx");
        }
    }
</script>

edit
instead of isNan should be isNaN (javascript is casesensitive)

Upvotes: 2

timothyclifford
timothyclifford

Reputation: 6959

Shouldn't this line...

if (isNan(textbox) == false)

be this instead...

if (isNan(textbox.value) == false)

Upvotes: 1

Related Questions