This is a migrated thread and some comments may be shown as answers.

server side validation check if field is empty

5 Answers 359 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Susanna Wiedemann
Top achievements
Rank 2
Susanna Wiedemann asked on 28 Jul 2009, 02:54 PM
Dear all

I added the validation (on server-side) for some fields of my grid. It's working fine.

But, I'd like to add, that if the field is blank/empty the validation fails as well. I found out, that when my code is running and the field is blank it's not running through the validation. Plelase find here the code of the asp and the vb I used:

          <telerik:GridTemplateColumn HeaderText="Zip Code" UniqueName="TemplateColumn">  
          <EditItemTemplate> 
          <asp:TextBox id="TextBox1" runat="server" Text='<%# Bind("zipcode") %>' MaxLength="4"></asp:TextBox> 
            <asp:CustomValidator id="CustomValidator1" runat="server"   
            ErrorMessage="please enter a zip code" 
            ControlToValidate="TextBox1" 
            OnServerValidate"CustomValidator1_ServerValidate">  
            </asp:CustomValidator> 
            </EditItemTemplate> 
            <ItemTemplate> 
             <asp:Label ID="label1" runat="server" Text='<%# Eval("zipcode") %>'></asp:Label> 
            </ItemTemplate> 
          </telerik:GridTemplateColumn> 

    Protected Sub CustomValidator1_ServerValidate(ByVal [source] As ObjectByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)  
        Dim i As Integer 
        Try 
            i = Convert.ToInt16(args.Value)  
        Catch ex As Exception  
            args.IsValid = False 
        End Try 
 
        If args.Value.Length <> 4 Then 
            args.IsValid = False 
        End If 
    End Sub 'CustomValidator1_ServerValidate 

As already said, it's working fine, it's just not running the CustomValidator1_ServerValidate code if the field zipcode is left blank by the user.

Does anybody has some ideas, solutions or is there anything I did wrong?

Thanks in advance for your help

Cheers

Marino


5 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 31 Jul 2009, 09:17 AM
Hello Marino,

Try removing the ControlToValidate setting in the custom validator and see if it makes any difference.
Let me know how it goes.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Susanna Wiedemann
Top achievements
Rank 2
answered on 03 Aug 2009, 07:36 AM
Hi Iana

Thanks for answer. It is working when the ControlToValidate is removed - the control is validated if it's empty.

It is very unclear for me, why it is working when this property is empty. Could you please explain?

Thanks a lot for your help

Marino
0
Sebastian
Telerik team
answered on 03 Aug 2009, 08:27 AM
Hello Marino,

I thin this is how the asp CustomValidator behaves when you wire its OnServerValidate event, assign a control to be validated by it and leave it blank. Does this behavior change when you move the textbox and the custom validator outside of the edit template of RadGrid template column or implement the same scenario inside MS GridView's template field?

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Accepted
Susanna Wiedemann
Top achievements
Rank 2
answered on 16 Sep 2009, 11:08 AM
Dear all

After I added filter options the validation wasn't working at all with the removed "ControlToValidate".

I found a parameter that helped me to solve my problem. ValidateEmptyText needs to be set on true:
          <telerik:GridTemplateColumn HeaderText="Zip Code" UniqueName="TemplateZip" 
          AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" 
            ShowFilterIcon="false" DataField="Zipcode"  > 
            
           <ItemTemplate> 
            <asp:Label ID="label1" runat="server" Text='<%# Eval("zipcode") %>'></asp:Label> 
           </ItemTemplate> 
              
           <EditItemTemplate> 
            <asp:TextBox id="TextBox1" runat="server" Text='<%# Bind("zipcode") %>'  MaxLength="4"   
              CausesValidation="true" ></asp:TextBox> 
             <asp:CustomValidator id="CustomValidator1" runat="server" 
              ErrorMessage="please enter a swiss zip code" 
              ControlToValidate="TextBox1" 
              ValidateEmptyText="true" 
              OnServerValidate"CustomValidator1_ServerValidate">  
             </asp:CustomValidator> 
            </EditItemTemplate> 
          </telerik:GridTemplateColumn> 

When I added this I could use the ControlToValidate again and it is validating the field correctly now, even if the field is left blank.

Regards

Marino

edit: can somebody mark this as the answer, pls?! I can't mark my own posts as answer... Thx
0
Sebastian
Telerik team
answered on 16 Sep 2009, 11:36 AM
Hello Marino,

Thank you for sharing your solution in this forum thread - thus you can help other community members who are trying to implement similar solution. I marked your post as an answer and updated your Telerik points for the involvement.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Susanna Wiedemann
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Susanna Wiedemann
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or