Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 842
  • Last Modified:

web textbox validation

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.
1 Solution
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...

  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.
<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>
Independent Software Vendors: 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!

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.

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.
oops.. it works that way for <input> fields, if you use <textarea> objects then you gota use innerText property instead of value

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now