views:

16

answers:

0

I want to use ajax with validator control.

for example, the various error messages should be show when a user input his/her name in a textbox to register:

  1. if nothing is input, "you should input your name";
  2. if not correct format, "your username is not valid formatted";
  3. if the username has used by other user, "the usename has been used by others"

1 and 2 are simple, since RequiredFieldValidator and RegularExpressionValidator is qualified. Problem comes the 3rd one, because I need check it in Database. So I decide to use WebMethod provided by Asp.net Ajax and CustomValidator together.

It's OK about the WebMethod except it return a callback handler but a variable. so I have to code like this:

function chkImgCodeCorrect(sender, args) {
    Freeflying.WebService.SUser.CheckUsernameExist($get("txtUserName").value, callBack, null, sender); }

function callBack(result, sender) {
    if (result == true) {  
        // lblUserName is used to show error message
        $get("lblUserName").innerText = "username has been used";
    }
    else {
        $get("lblUserName").innerText = "";
    } }

but it can not stop the submit if the username is existed, so I use CustomValidator and create a client function like the following:

function stopSubmit(sender, args) {
    if ($get("lblUserName").innerText != "") {
        sender.IsValid = false;
    }
    else {
        sender.IsValid = true;
    }
}

But once leaving out the focus, Ajax method is triggered first, which cause some performance hit and other downside. And whatever, it's too ugly!

I think there should be some very mature solution since it has been used for a long time by so many websites.

Help me, please!

related questions