Is a submit button pressed in ASP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • F159753
    New Member
    • Apr 2008
    • 31

    #1

    Is a submit button pressed in ASP?

    Hi,

    I have 2 text box and a "search " button.

    I would like to check if the text boxes are empty [U]when the search button has been pressed. How should I do that?

    Regards,
    FF
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Originally posted by F159753
    Hi,

    I have 2 text box and a "search " button.

    I would like to check if the text boxes are empty [U]when the search button has been pressed. How should I do that?

    Regards,
    FF
    Hi FF,

    You need to use a bit of javascript to check the contents of the textboxes prior to the form being submitted. First place a javascript function between the head tags of your page which contains the validation then call that function in the onclick event of your submit button. Use this example to help you:

    Code:
     <head> 
    <script type="text/javascript">
    function Val()
    {
    var txt1 = document.Form1.Textbox1.Value;
    if (txt1 == '')
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    </script>
    </head>
    <body>
    <form name="Form1">
    <input type="text" name="TextBox1" />
    <input type="submit" onclick="return Val();" />
    </form>
    </body>
    Hope this helps.

    Dr B

    Comment

    • jeffstl
      Recognized Expert Contributor
      • Feb 2008
      • 432

      #3
      What I'm posting here is not exactly a great solution necessarily (javascript is definitly ideal in terms of a users experience and programing logic) but as an additional option or alternative if you don't want to mess with javascript or you need more flexibility with error messages, etc you can check if the boxes are empty on your submit page and redirect them back to the original page if they are empty.

      So your submit page would have (assuming BOTH text boxes are blank when you send them back:

      Code:
      dim FirstText, SecondText
      
      FirstText = request.form("txtBox1")
      SecondText = request.form("txtBox2")
      
      If FirstText = "" then
           If SecondText = "" then 'send them back
                resposnse.redirect("SearchPage.asp?err=Blanks")
           end if
      end if
      Then on your original search page you use the err variable to display a message

      Code:
      if request.querystring("err") <> "" then
           response.write "Fields cannot be blank!!"
      end if
      To go a step further for anyone else too, you can pass your user filled data back and forth either in session objects or querystrings if you want to be able to retain the data the user entered on the search page (ie return them to the page with the form filled out rather then clearing it out again)

      Comment

      Working...