javascript to check the text of a textbox and then continue

Discussion in 'ASP .Net' started by buran, Mar 26, 2005.

  1. buran

    buran Guest

    Dear ASP.NET Programmers,

    I have a question about a script I'm trying to code and invoke when a button
    (btnSave) is pressed on the page. This script should only run when a textbox
    (txtAD) on the page is left blank. I tried to use a code snippet with the
    "return confirm" function but without success. The code should check whether
    the textbox is empty or not, and if empty, it should ask the user to
    continue and then run the next code. How can I accomplish this? Thanks in
    advance,

    Buran
    buran, Mar 26, 2005
    #1
    1. Advertising

  2. buran

    JustinBlat Guest

    Buran,
    you're going to want to capture the client onclick event of your save
    button, and run the script from there. First, in your codebehind
    append the client event handler:

    btnSave.Attributes.Add("OnClick","return CheckForEmptyTextbox();");

    Now the javascript function CheckForEmptyTextbox will be called when
    the button is clicked. This function needs to return a boolean value,
    telling the page whether or not to submit the form.

    <script language="JavaScript">
    function CheckForEmptyTextbox() {
    var continue;
    var txtAD;

    // first get a reference to the textbox object
    txtAd = document.getElementById('txtAD');

    // check if the textbox is blank
    if (txtAD.value == '') {

    // since the textbox is blank, ask the user if they want to
    continue
    continue = confirm('txtAD is blank, do you want to continue?');

    return continue;

    } // end if

    // since the value is filled in, just return true
    return true;

    } // end function
    </script>

    buran wrote:
    > Dear ASP.NET Programmers,
    >
    > I have a question about a script I'm trying to code and invoke when a

    button
    > (btnSave) is pressed on the page. This script should only run when a

    textbox
    > (txtAD) on the page is left blank. I tried to use a code snippet with

    the
    > "return confirm" function but without success. The code should check

    whether
    > the textbox is empty or not, and if empty, it should ask the user to
    > continue and then run the next code. How can I accomplish this?

    Thanks in
    > advance,
    >
    > Buran
    JustinBlat, Mar 27, 2005
    #2
    1. Advertising

  3. buran

    buran Guest

    Dear JustinBlat,

    I tried your code, but unfortunately I could not manage it to work.

    In my code-behind:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    btnSave.Attributes.Add("OnClick", "return
    CheckForEmptyAdmissionDate();")
    LoadControls()
    If Not Page.IsPostBack Then
    LoadData()
    End If
    End Sub

    Then, in my aspx page:

    <script language="JavaScript">
    function CheckForEmptyAdmissionDate() {
    var continue;
    var txtAD;
    txtAD = document.getElementById('txtAD');
    if (txtAD.value == '') {
    continue = confirm('Admission Date is blank, do you want to
    continue?');
    return continue;
    }
    return true;
    }
    </script>

    When I press the button (btnSave), the page is submitted although the txtAD
    textbox is empty. What may cause this problem?
    Thanks in advance,

    Buran


    "JustinBlat" <> wrote in message
    news:...
    > Buran,
    > you're going to want to capture the client onclick event of your save
    > button, and run the script from there. First, in your codebehind
    > append the client event handler:
    >
    > btnSave.Attributes.Add("OnClick","return CheckForEmptyTextbox();");
    >
    > Now the javascript function CheckForEmptyTextbox will be called when
    > the button is clicked. This function needs to return a boolean value,
    > telling the page whether or not to submit the form.
    >
    > <script language="JavaScript">
    > function CheckForEmptyTextbox() {
    > var continue;
    > var txtAD;
    >
    > // first get a reference to the textbox object
    > txtAd = document.getElementById('txtAD');
    >
    > // check if the textbox is blank
    > if (txtAD.value == '') {
    >
    > // since the textbox is blank, ask the user if they want to
    > continue
    > continue = confirm('txtAD is blank, do you want to continue?');
    >
    > return continue;
    >
    > } // end if
    >
    > // since the value is filled in, just return true
    > return true;
    >
    > } // end function
    > </script>
    >
    > buran wrote:
    > > Dear ASP.NET Programmers,
    > >
    > > I have a question about a script I'm trying to code and invoke when a

    > button
    > > (btnSave) is pressed on the page. This script should only run when a

    > textbox
    > > (txtAD) on the page is left blank. I tried to use a code snippet with

    > the
    > > "return confirm" function but without success. The code should check

    > whether
    > > the textbox is empty or not, and if empty, it should ask the user to
    > > continue and then run the next code. How can I accomplish this?

    > Thanks in
    > > advance,
    > >
    > > Buran

    >
    buran, Mar 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. - Steve -
    Replies:
    3
    Views:
    400
    Simon Turner
    Jul 29, 2003
  2. Mufasa
    Replies:
    7
    Views:
    413
    Alexey Smirnov
    Sep 4, 2007
  3. Replies:
    0
    Views:
    280
  4. Kafer
    Replies:
    3
    Views:
    158
    Kafer
    Oct 8, 2003
  5. Replies:
    4
    Views:
    544
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page