views:

37

answers:

3
<input id='btnExcelRead' name='btnExcelRead' type='submit' runat='server'/>   <- actually asp:button
<input id='excelUpload' name='excelUpload' type='file' />   
<input id='txtStartDate' type='text' />
<input id='txtEndDate' type='text' />

..

$(function(){

          $("#btnExcelRead").click(CheckValidation);

        });

        var CheckValidation = function() {
            if ($("#excelUpload").val() === "") {
                alert("Select file");
                return false;
            }
            if ($("$txtStartDate").val() === "") {
                alert("Check the start date!");
                return false;
            }
            if ($("$txtEndDate").val() === "") {
                alert("Check the end date!");
                return false;
            }
        }

here i made simple jquery code.

I want to bind function when btnExcelRead button click.

is this originally wrong way?

A: 
$("#btnExcelRead").click(function(){CheckValidation()});
griegs
Yuck! WHY add an extra function that does nothing but call an existing function? It changes the context of `this`, but in this case that's not a consideration.
VoteyDisciple
This doesn't help, you're just adding an unnecessary function wrapper...
Nick Craver
This doesn't work correctly. doesn't pass return value of the `CheckValidation`
sunglim
+2  A: 

What you have is valid, aside from the selectors, I would reformat is a bit, like this:

$(function(){
      $("#btnExcelRead").click(CheckValidation);
});

function CheckValidation () {
    if ($("#excelUpload").val() === "") {
        alert("Select file");
        return false;
    }
    if ($("#txtStartDate").val() === "") {
        alert("Check the start date!");
        return false;
    }
    if ($("#txtEndDate").val() === "") {
        alert("Check the end date!");
        return false;
    }
}

You can see a demo of it working here

You have $txtStartDate and $txtEndDate for your selectors, I think you meant #txtStartDate and #txtEndDate here (I assume you're finding them by ID). Also if you want a named function, just make one :) If you store a variable pointing to a anonymous function, make sure to put a ; after it, since that's a statement.

Nick Craver
also, returning false makes no sense here
colinmarc
@colinmarc: Sure it does, what if that's a submit button for the form?
Nick Craver
A: 

You have to declare the callback before calling it like follows:

    var CheckValidation = function() {
        if ($("#excelUpload").val() === "") {
            alert("Select file");
            return false;
        }
        if ($("$txtStartDate").val() === "") {
            alert("Check the start date!");
            return false;
        }
        if ($("$txtEndDate").val() === "") {
            alert("Check the end date!");
            return false;
        }
    }

    $(function(){

      $("#btnExcelRead").click(CheckValidation);

    });
GeekTantra
This is not true, see a demo showing this: http://jsfiddle.net/SshdM/
Nick Craver
Nick, your code in that demo is different than what GeekTantra and I both posted in one key respect: you've translated the `var CheckValidation = function()` format into the `function CheckValidation()` format. See this demo for the version as GeekTantra, sunglim, and I all wrote it: http://jsfiddle.net/p357H/
VoteyDisciple
@VoteyDisciple: That's because jsfiddle is wrapping your code in `docuemnt.ready`, since you've selected `onLoad` on the left, change it to "No wrap", like this: http://jsfiddle.net/4xUMA/ Then the variable gets defined before `document.ready` runs.
Nick Craver
Not my downvotes here, rarely do I ever, but you should keep certain things in mind, mainly the possibility that the problem's completely unrelated to what *looks* like the problem, e.g. selectors.
Nick Craver
@Nick - Wow, you *really* don't down-vote. I mean 6 out of 1,890? Come on man! Live a little! ;o)
patrick dw