tags:

views:

84

answers:

2

Hi,

I have an user registration on my site.

I want to look it cool, so my plan is, that after a textbox is left, a green or red sign right behind the textbox tell the user if e.g. the username is unused, the email-adress is unused, the password is correct entered twice and so on.

Yes, I know the validation controls, but there are only a bunch of functions, isn't it? E.g. for checking if the email-adress is unused I must check by database and so on...

Any Ideas?

+1  A: 

I would wrap the whole thing in an update panel, this is something I do quite often...

<asp:scriptmanager runat="server" id="sm1" />
<asp:updatepanel runat="server" id="up1" updatemode="Conditional">
<contenttemplate>
    <asp:textbox runat="server" id="tbUsername" autopostback="true" ontextchanged="tbUsername_TextChanged" />
    <asp:customvalidator runat="server" text="Email already used" id="cusValEmail" />
    <asp:textbox runat="server" id="tbPassword"  />
</contenttemplate>
</asp:updatepanel>

and in the code behind

protected void tbUsername_TextChanged(object sender, EventArgs e)
{
 //call DB etc and mark validator as needed
 cusValEmail.IsValid = false;
}

The key is setting the textbox autopostback to true and utilising the ontextchanged event.

Rippo
Can you explain it for an newbie in this a little bit longer?- Why the scriptmanager and updatepanel? (Until now I only a few textboxes and a button)- But isn't the text-changed method not to often? I mean every time he tipe a letter to send a query to the DB is... quite often- Is showing the error just-in-time or when the user hits the send-button?
Kovu
The ontextchanged event only happens when the control loses focus.I suggest you create this page and try it out. It does not matter that you only have a few textboxes and a button.. The error will be shown as you want when the control loses focus....HTH
Rippo
It is as I wishied, wunderful!Thank you
Kovu
Great! Glad it works... Good luck
Rippo
A: 

A simple way of doing this would be to create a controller action (if you are using MVC) or a page (if you are using regular asp/asp.net) that you post the username, e-mail address, etc to that returns a simple plain-text colour value - red or green, depending on whether all the parameters have been posted are ok. You could then apply that to the colour-style of the box. If you don't have/want that to be an https call then I probably wouldn't include the passwords in that, you ajax return code could maybe say

if (<password are equal>)
{
  set style-colour to the return value
}
else
{
  set to red
}

or probably, even better, something like

if (<passwords are equal>)
{
  run ajax call
}
else
{
 set style-colour to red
}

and the the ajax return sets the colour to the return value, that way you also save a round trip to the server if the passwords aren't equal

Any reason you want/need to do this via ajax rather than a regular postback (you could use an update panel to do a partial postback)?

Mad Halfling
I am interested in the AJAX side of this? How would you implement it?
Rippo