custom validator

cocol

Active member
Joined
Jul 23, 2010
Messages
30
Programming Experience
Beginner
hello!
i need help with custom validator,so when i have a requiredfiledvalidator for a textbox which autopostback=true ,so if the textbox is empty then i saw the error message,but with custom validator it doesn't work even when i place a button and i click on it so it doesn't work or to be cleared sometimes worked and then no .
but i need for the custom validator to work in autopostback property of the textbox not when i press a button and i want that this custom validator to make sure that the textbox is not empty.
so i need help .
thank you.
 
An example:


PHP:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" />
            <asp:RequiredFieldValidator
            ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="This field is required" ControlToValidate="TextBox1" 
            EnableClientScript="False" />
        <asp:Button ID="ValidateButton" runat="server" Text="Validate" />
    </div>
    </form>
</body>

then in your code behind:
PHP:
    Protected Sub ValidateButton_Click(ByVal sender As Object, _
                         ByVal e As System.EventArgs) Handles ValidateButton.Click
        If IsValid Then
            '' Do something
        Else
            '' Do something
        End If
    End Sub
 
thank you but i need the custom validator because i am trying to create the "create user" so for the textbox for the e-mail i want a custom validator and the username to be sure that the length of the user not <5 so the required field validator not what i need,and the problem with custom validator that with the autopostback=true with the textbox because only when you click the button and the other annoying problem that when i set args.isvalid=false and i test in the button if the page isvalide and i found true,it's annoying because when the sub for the custom validator ends,the page reset to true so what i need that how to call the function or sub of custom validator from a button to set manually if the page is valide or not and not to be reset when the sub finished.
thank you.
 
ok i tried and i tried and i searched in the internet but i need to know how i can call the sub of the customvalidator from a button and this is my example:
VB.NET:
    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        If args.Value = "" Then
            args.IsValid = False
           End If
      If TextBox1.Text = "" Then
          args.IsValid = False
          
        End If
        If args.Value.Length > 20 Then
            args.IsValid = False
            End If
    End Sub



 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Page.Validate()
        CustomValidator1_ServerValidate(sender, New System.Web.UI.WebControls.ServerValidateEventArgs("TextBox1", False)) 
        MsgBox(Page.IsValid)
       If Not Page.IsValid Then
        else
'code here = insert in database
            
        End If
End Sub

so first page.validate() don't work so i tried second=>second when i call the sub " CustomValidator1_ServerValidate" i don't know what's the value means so i put textbox1,and false or true the same result = page.isvalid=true always ,but in the suv " CustomValidator1_ServerValidate" it change to false but returned true :S.
so i i have several custom validator in different updatepanel ,so what i need that to validate each textbox of the create USER wizard and when l click the button i want to validate all the page first and that=>page.validate() doesn't call the sub of the custom validator.
thank you :)
 
Back
Top