By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Is a submit button pressed in ASP?

P: 31
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
Jun 9 '08 #1
Share this Question
Share on Google+
2 Replies


DrBunchman
Expert 100+
P: 979
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:

Expand|Select|Wrap|Line Numbers
  1.  <head> 
  2. <script type="text/javascript">
  3. function Val()
  4. {
  5. var txt1 = document.Form1.Textbox1.Value;
  6. if (txt1 == '')
  7. {
  8. return false;
  9. }
  10. else
  11. {
  12. return true;
  13. }
  14. }
  15. </script>
  16. </head>
  17. <body>
  18. <form name="Form1">
  19. <input type="text" name="TextBox1" />
  20. <input type="submit" onclick="return Val();" />
  21. </form>
  22. </body>
  23.  
Hope this helps.

Dr B
Jun 9 '08 #2

jeffstl
Expert 100+
P: 432
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim FirstText, SecondText
  3.  
  4. FirstText = request.form("txtBox1")
  5. SecondText = request.form("txtBox2")
  6.  
  7. If FirstText = "" then
  8.      If SecondText = "" then 'send them back
  9.           resposnse.redirect("SearchPage.asp?err=Blanks")
  10.      end if
  11. end if
  12.  
  13.  
Then on your original search page you use the err variable to display a message

Expand|Select|Wrap|Line Numbers
  1.  
  2. if request.querystring("err") <> "" then
  3.      response.write "Fields cannot be blank!!"
  4. end if
  5.  
  6.  
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)
Jun 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.