webform: how to prevent user from inserting empty fields

G

Guest

Hi,

I am building a form out of simple text boxes, dropdownlists etc. and a
submit button.

Currently the form accepts empty fields of data. How can I prevent a user
from being able to insert empty data, i.e. something that checks if any of
the fields are empty (and if so, lets the user have another try) before
submitting??

Thanks!
 
D

Dirc Khan-Evans

Ido said:
Hi,

I am building a form out of simple text boxes, dropdownlists etc. and
a submit button.

Currently the form accepts empty fields of data. How can I prevent a
user from being able to insert empty data, i.e. something that checks
if any of the fields are empty (and if so, lets the user have another
try) before submitting??

Thanks!

Use one of the validator controls that come with ASP.NET
Place them on the form and set the ControlToValidate property.

For more information take a look at MSDN.

Dirc

--
 
G

Guest

Hi,

Indeed I have used these controls; and they display a message (e.g. "please
enter name") when they detect that the field is empty. However, when I press
'submit' the data is still inserted into the datasource
('sqldatasource.Insert()').
How can I _force_ users to type something in before inserting it?

In addition, I would like to check for more properties of the text in the
text box (for example whether it is of the right type).


Thanks for your time...
Ido
 
D

Dirc Khan-Evans

Ido said:
Hi,

Indeed I have used these controls; and they display a message (e.g.
"please enter name") when they detect that the field is empty.
However, when I press 'submit' the data is still inserted into the
datasource ('sqldatasource.Insert()').
How can I force users to type something in before inserting it?

In addition, I would like to check for more properties of the text in
the text box (for example whether it is of the right type).


Thanks for your time...
Ido



You need to set them to validate client side. The control will then
emit the relevant validation java script and stop the post back until
valid data is entered... to do this set EnableClientScript property of
the control to true.

As for the text, if you have complex requirements, you might consider a
regular expression validator.

Best practice is to also do the validation server side, just in case
someone tries to post dud information. you can either do this in the
page_load handler for the post back or you can do this by adding a
handler to the ServerValidate event of the control.



--
 
G

Guest

You would on the server side, check fr Page.IsValid before submitting your
web forms page.
Something like this
public void btn_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
//Insert values
}
}

Page.IsValid is set to be true only when all your validators in your form
pass their validation
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top