web textbox validation

Posted on 2005-03-11
Medium Priority
Last Modified: 2008-02-01
I have a webpage that has 2 textboxes and 1 submit button. When the user hits the submit button it calls another file that submits data to an email account and then the page displays saying sent....

I need something to make sure the textboxes have data in it before it calls the other file.

any ideas.
Question by:bear23
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Expert Comment

ID: 13522936
Hi bear23:
From the Toolbox, drag a RequiredField Validator to your form
Set its ControlToValidate property to one of your textboxes name, and Type into the Error Message property the message you want to appear.
Repeat the process for your other textbox

This is assuming you are using ASP.NET...


Expert Comment

ID: 13523133
  What languages are you using? If you use JavaScript, your user can disable JavaScript and do what they don't expected to do. So I think it is Server side script. Is it PHP, Perl, ASP.NET, JSP .... Please tell more details.
LVL 29

Accepted Solution

rdivilbiss earned 200 total points
ID: 13523291
<form method="post" action="nextPageToSendEmail.asp" onsubmit="return validate(this);">
   <input type="text" name="textbox1" ...>
   <input type="text" name="textbox2" ...>
   <input name="submit" type="submit" value="Submit">

function validate(oForm) {
    var message='';

    if (oForm.textbox1.value=='') {
        message='You must enter data in TextBox1\n';
    if (oForm.textbox2.value=='') {
        message=message + 'You must enter data in TextBox2';
    if (message=='') {
        return true;
    } else {
        return false;

On the next page that sends e-mail you should also check for data.

If it is ASP, then before the <HTML> tag....

Dim okayToSend
okayToSend = true
if request.form("textbox1") = "" then
    okayToSend = false
end if
if request.form("textbox2") = "" then
    okayToSend = false
end if
if NOT okayToSend then
end if

.... code to send the e-mail

<HTML> .... </HTML>
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13523663
Hi bear,

Just an additional note:
In general there are always two possibilities to validate a form:
a) client side
b) server side

The expert rdivilbiss shows how to use both methods. First he validates the form using javascript (client side) and then, using ASP, doing the validation (well - a check for existance) on the server side.

Javascript validation depends on the users settings and it is NOT a secure validation (turn off javascript get rid of any validation). Server side validation is (and that's my personal view) a secure, proper thing to do - but a far way to go just to validate a simple form field :) .
Best practice: use both, as rdivilbiss suggests - javascript is user friendly and produces for the user a fast answer - server side is proper and more secure.

BTW: if its some formmailer stuff - watch out that it can't be abused by some Bots to send out spam mails. Do this on the server side.


Expert Comment

ID: 13547129
you can use the onsubmit property of your form to do so, in ex:

onsubmit="return !(field_a.value==''||field_b.value=='')"

note: '' is twice the ' (empty string), dont confuse them with "
the value returned to onsubmit method is ignored by some browsers witch will submit the form anyway, it works fine on IE
in any case its a good idea to check them in the server again.

complete example:
<form onsubmit="return !(field_a.value==''||field_b.value=='')">
<input id=field_a><br>
<input id=field_b><br>
<input type=submit>

you can have a more elaborate function to validate the form and present messages in error case, just do a validation function and then call it from the onsubmit prop.

Expert Comment

ID: 13547160
oops.. it works that way for <input> fields, if you use <textarea> objects then you gota use innerText property instead of value

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question