Customize the client side validation in asp.net

S

Sun Jian

Hi,

I am trying to customize the asp.net validation to achieve the following:

Upon submitting the form, client side validation will run, and it will stop
at the first error detected. For example if both UserID and Password text
fields are required but neither is filled in, I'd like to display the error
message (a dialogbox) "Please enter the User ID". And only after the user
has filled in UserID, it will display "Please enter the Password".

Initially I thought this should be an easy thing to do (I still believe so),
but I have spent a few hours and still could not find a good solution. The
validator controls / summary controls are too inflexible to achieve this. I
am willing to write all the validation logic myself, but I don't know how do
I hook my logic to the existing client validation flow.

Any help is appreciated.

OnlyGo
 
A

Andrei Pociu

Hello,

I'm not sure you can do that with the ASP.NET validation controls.

But if you do your own validation you can easily accomplish what you want
using JavaScript such as this one:

function LengthCheck()
{
if (document.formName.txtName.value.length == 0)
{
alert("You must enter a name.");
return false;
}
if (document.formName.txtPassword.value.length == 0)
{
alert("You must enter a password.");
return false;
}
}

And you need a form tag like:

<form action="file.aspx" method="post" name="formName" onSubmit="return
LengthCheck()">

Regards,
Andrei
 
P

Paul Glavich [MVP ASP.NET]

You should be able to easily do this with the validator controls. Using the
validation summary, you should be able to get a popup message displaying
what you require also. On the ValidationSummary control, enable the
'ShowMessageBox' property to get the dualog box. Ad some requiredfield
validators for each field you require and sets its error msg property
appropriately. Thats about it.
 
B

Brock Allen

As already mentioned the built in validation controls allow most of what
you're looking for. The <asp:CustomValidator> allows you to write you own
custom client side javascript validation and server side validation code
too (You should always do the server side piece, as a client can disable
javascript). Also there is the <asp:ValidationSummary> that can do message
boxes if you'd like or HTML to show the user a list of the errors. The one
aspect in which it diverges from what you're looking for is that the summary
will show all current errors on the page, as opposed to just the first one
it encounters. If you want to use a framework to expediate your development
process, you sometimes have to defer to its particular quirks. If you don't
like how it works, then you can build your own, but then you're going to
be spending more time in development.
 
S

Sun Jian

Thanks Andrei, that works. I was trying the similar approach by hooking up
the validation function to the submit button itself. And because of some
validation control, the submit button has already had a onclick() handler
and that's why I don't know how my function can be hooked up to that.

The form onSubmit handler completely solves my problem.

Thanks again.

Sun Jian
 
S

Sun Jian

Thanks Brock and Paul!

I understand fully the trandoffs between rolling out my own framework and
using the existing one. There are actually 2 reasons I wanted to do this.
One reason is that I am doing some javascript/DHTML stuff in my page that
conflicts with the client side validation script from asp.net, which is very
messy to debug. Another reason is that I have heard many times that asp.net
is very extensible. And I wanted to take this opportunity to learn by doing
things that are not designed to work by default ;)

Sun Jian
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top