Im facing trouble with a custom validator that doesn't wanna fire. I have this form with some textboxes, asking the user for information. All txtbox are requiered and wel the Required field validator behave has expected. For the email textboxes i have
a compared validator which also work as expected. But I have this USERNAME textbox which is giving me the trouble. It has 3 validators: a required field validator, a regular expressionvalidator and a custom validator. The first 2 work and behave as expected.
At one point the custom validator also worked but it stop working at some point when my teammates place more code into the webform (my guess) Here is the html code and the code behind the custom validator:
SMWAv3.Data.DS_UserAdminTableAdapters.MiscQrys UC = new SMWAv3.Data.DS_UserAdminTableAdapters.MiscQrys(); args.IsValid = false;
int returnValue = (int)UC.TheUserCount(txt_UserName.Text);
if (returnValue == 0)
{
args.IsValid = true;
}
}
The custom validator is supost to go into the database and check that the username input in the textbox is doesn't exist, if it exist then show the * letting the user know that username is invalid. FYI there is no more code in the page since at the moment
that validator is the only code needed for now.
Well everything but the txt is on a validation group named New USER, but thats for the Validation summary so it can display the errors once there is a postback.. What I did was conver the validation into a function and call it from the .Click event of the
submit button and place a break point. From what I saw the validation did fire and did work. At the moment I only ahve one user called ADMINISTRATOR. What I did in this case was, fill everything up and use ADMINISTRATOR as username hoping it will tell me
the user is taken, when I use this method and force the validation it does work, so the code works, like before. What I want is for the custom validator to work like the other and like before ... when it looses focus, so the user dont have to enter everything
just to find out the username is taken.. In the end if nothing else works, it can be this way, but I just want to learn why is not working like the other validators
I took the function out and took the call from the .click event. This time I comment the responce.redirect from the submit button and Bingo, the validator did fire and did it's work and told me the user is taken. But well, I need the responce.redirect
so when they submit the page get redirected. So now I need logic as to how to fix this issue. If I leave the responce.redirect it does the validation but it continue with the action of the event instead of stopping
Well I got a fix now ... on the click event I placed
If (page.valid) and then execute the response.redirect..
this solves my issue.. but it doesn't kill my curiosity as to why the custom validator don't work onblur or on lost of focus... unless that control simply can't work like that ><
Marked as answer by gindy39 on Feb 14, 2012 06:04 PM
Gindy39
Member
246 Points
99 Posts
Custom validator not firing
Feb 14, 2012 03:20 PM|LINK
Good day guys and gals,
Im facing trouble with a custom validator that doesn't wanna fire. I have this form with some textboxes, asking the user for information. All txtbox are requiered and wel the Required field validator behave has expected. For the email textboxes i have a compared validator which also work as expected. But I have this USERNAME textbox which is giving me the trouble. It has 3 validators: a required field validator, a regular expressionvalidator and a custom validator. The first 2 work and behave as expected. At one point the custom validator also worked but it stop working at some point when my teammates place more code into the webform (my guess) Here is the html code and the code behind the custom validator:
<asp:TextBox ID="txt_UserName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegVal_UN_length" runat="server" ControlToValidate="txt_UserName" ErrorMessage="Minimum username length is 3" ValidationExpression=".{3}.*" ValidationGroup="NewUser" Text="*" ForeColor="Red" Display="Dynamic"/>
<asp:RequiredFieldValidator ID="ReqVal_User" runat="server" ErrorMessage="Username is required." ControlToValidate="txt_UserName" Text="*" ValidationGroup="NewUser" ForeColor="Red" Display="Dynamic"/>
<asp:CustomValidator ID="CusVal_Username" runat="server" ControlToValidate="txt_UserName" OnServerValidate="CusVal_Username_ServerValidate" ErrorMessage="Username is already used" Text="*" ValidationGroup="NewUser" ForeColor="Red" Display="Dynamic" />
***code behind***
protected void CusVal_Username_ServerValidate(object source, ServerValidateEventArgs args)
{
SMWAv3.Data.DS_UserAdminTableAdapters.MiscQrys UC = new SMWAv3.Data.DS_UserAdminTableAdapters.MiscQrys(); args.IsValid = false;
int returnValue = (int)UC.TheUserCount(txt_UserName.Text);
if (returnValue == 0)
{
args.IsValid = true;
}
}
The custom validator is supost to go into the database and check that the username input in the textbox is doesn't exist, if it exist then show the * letting the user know that username is invalid. FYI there is no more code in the page since at the moment that validator is the only code needed for now.
Thank you for your help.
monts_hacker
Member
53 Points
27 Posts
Re: Custom validator not firing
Feb 14, 2012 04:34 PM|LINK
Very Simple Make A Gropup Vaidator Control & Set all Validation on this :-
THis may Help You very much :-
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_prop_webcontrol_imagebutton_validationgroup
http://www.w3schools.com/aspnet/prop_webcontrol_imagebutton_validationgroup.asp
OK ??
Still if you can't solve it , feel free to contact me :-
shah11592@gmail.com
MONTS
Gindy39
Member
246 Points
99 Posts
Re: Custom validator not firing
Feb 14, 2012 04:59 PM|LINK
Well everything but the txt is on a validation group named New USER, but thats for the Validation summary so it can display the errors once there is a postback.. What I did was conver the validation into a function and call it from the .Click event of the submit button and place a break point. From what I saw the validation did fire and did work. At the moment I only ahve one user called ADMINISTRATOR. What I did in this case was, fill everything up and use ADMINISTRATOR as username hoping it will tell me the user is taken, when I use this method and force the validation it does work, so the code works, like before. What I want is for the custom validator to work like the other and like before ... when it looses focus, so the user dont have to enter everything just to find out the username is taken.. In the end if nothing else works, it can be this way, but I just want to learn why is not working like the other validators
ty
Gindy39
Member
246 Points
99 Posts
Re: Custom validator not firing
Feb 14, 2012 05:48 PM|LINK
Update...
I took the function out and took the call from the .click event. This time I comment the responce.redirect from the submit button and Bingo, the validator did fire and did it's work and told me the user is taken. But well, I need the responce.redirect so when they submit the page get redirected. So now I need logic as to how to fix this issue. If I leave the responce.redirect it does the validation but it continue with the action of the event instead of stopping
Gindy39
Member
246 Points
99 Posts
Re: Custom validator not firing
Feb 14, 2012 06:04 PM|LINK
Well I got a fix now ... on the click event I placed
If (page.valid) and then execute the response.redirect..
this solves my issue.. but it doesn't kill my curiosity as to why the custom validator don't work onblur or on lost of focus... unless that control simply can't work like that ><