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

Validate Text Length

7 Answers 419 Views
ValidationProvider
This is a migrated thread and some comments may be shown as answers.
Alessio Bulleri
Top achievements
Rank 1
Alessio Bulleri asked on 03 Jun 2020, 12:44 PM
How can I create (by code) a validation rule to test if the length of the text of a control is less then a maximun value or is between a minimun and a maximun value ?

7 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Jun 2020, 01:03 PM

Hello, Alessio,

Please refer to the following help article which demonstrates how to add different validation rules via code specifying the control, the property name, operator and value to compare with: https://docs.telerik.com/devtools/winforms/controls/validation-provider/validation-rules 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Alessio Bulleri
Top achievements
Rank 1
answered on 03 Jun 2020, 01:10 PM

Hi Dess, thanks for your help.

I've read the article but it doesn't explain how to check the text length of a control.

Can you please give me an example of a validation rule that checks if the text in a text box is less than 20 characters ?

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Jun 2020, 09:07 AM
Hello, Alessio, 

RadValidationRule allows you to define a rule for a certain control by specifying the PropertyName that has to be validated. However, RadTextBox doesn't offer a property that specifies how many symbols are entered in the editor. This can be checked by the RadTextBox.Text.Length property. 

The possible solution that I can suggest is to add a RadValidationRule for empty Text and in addition to this handle the ControlValidation event which occurs before a RadEditorControl is being validated. The RadValidationEventArgs gives very useful information about the tool tip error indication, validation rule, etc. The IsValid argument allows you to override the default result indicating whether the validation fails and change the error message accordingly:
        public RadForm1()
        {
            InitializeComponent();

            RadValidationRule radValidationRule1 = new RadValidationRule();
            radValidationRule1.AutoToolTip = true;
            radValidationRule1.AddControl(this.radTextBox1);
            radValidationRule1.Operator = Telerik.WinControls.Data.FilterOperator.IsNotLike;
            radValidationRule1.PropertyName = "Text";
            radValidationRule1.ToolTipText = "Text is empty!";
            radValidationRule1.Value = "";

            radValidationProvider1.ValidationRules.Add(radValidationRule1);

            this.radValidationProvider1.ControlValidation += radValidationProvider1_ControlValidation;
        }

        private void radValidationProvider1_ControlValidation(object sender, RadValidationEventArgs e)
        {
            if (e.Control == this.radTextBox1 && this.radTextBox1.Text != string.Empty &&
                this.radTextBox1.TextBoxElement.TextBoxItem.TextLength < 20)
            {
                e.ErrorText = "Text is less than 20 characters !";
                e.IsValid = false;
            }
        }

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Alessio Bulleri
Top achievements
Rank 1
answered on 04 Jun 2020, 02:05 PM

Hi Dess, thanks for your help.

I will take this solution for the moment. Hope in the future validation rules will be expanded allowing more complex rules with custom validation formulas.

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Jun 2020, 06:12 AM
Hello, Alessio, 

It seems to be a reasonable request to support nested properties in RadValidationProvider, e.g. rule's PropertyName = Text.Length

I have logged it in our feedback portal by creating a public thread on your behalf. You can track its progress, subscribe for status changes and add your comments on the following link - feedback item.

I have also updated your Telerik points.


Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Fahmi
Top achievements
Rank 1
Veteran
answered on 10 Oct 2020, 08:23 PM

Hi Dess,

I tried to implement my own validation rules using the RadValidationProvider, but apprently the last rule override it all.

So I tried to set the main RadValidationRule as not null ( so always true ) and I've personalized the main rule as below:

I need then when click on "Save" button return whether the validation rules are respected or not ?

public RadForm1()
      {
          InitializeComponent();
          RadValidationRule FirstRule = ValdationRule_NotNull(TB_FirstField);
          radValidationProvider1.ValidationRules.Add(FirstRule);
 
          RadValidationRule SecondRule = ValdationRule_NotNull(TB_SecondField);
          radValidationProvider1.ValidationRules.Add(SecondRule);
          this.radValidationProvider1.ControlValidation += radValidationProvider_ControlValidation;
 
      }
 
      private void radValidationProvider_ControlValidation(object sender, RadValidationEventArgs e)
      {
          ValidationProvider_Personalized(e, TB_FirstField, FieldTypes.String, false);
          ValidationProvider_Personalized(e, TB_SecondField, FieldTypes.Integer, true);
      }
       
      public enum FieldTypes
      {
          Integer,
          Float,
          String,
          DropdownList
      };
 
      private RadValidationRule ValdationRule_NotNull(RadTextBox control)
      {
          RadValidationRule radValidationRule = new RadValidationRule();
          radValidationRule.AutoToolTip = true;
          radValidationRule.AddControl(control);
          radValidationRule.Operator = Telerik.WinControls.Data.FilterOperator.IsNotNull;
          radValidationRule.PropertyName = "Text";
          radValidationRule.ToolTipText = "The field " + control.AccessibleDescription + " is null";
          radValidationRule.ToolTipTitle = "";
          radValidationRule.Value = "";
          return radValidationRule;
      }
 
      private void ValidationProvider_Personalized(RadValidationEventArgs e, Control FieldToValidateControl, FieldTypes FieldType, bool IsRequired = false)
      {
          RadDropDownList DropdownToValidate = new RadDropDownList(); ;
          RadTextBox TextBoxToValidate = new RadTextBox() ;
          if (FieldType != FieldTypes.DropdownList)
          {
              TextBoxToValidate = (RadTextBox)FieldToValidateControl;
              FieldToValidateControl = TextBoxToValidate;
          }
          else
          {
              DropdownToValidate = (RadDropDownList)FieldToValidateControl;
              FieldToValidateControl = DropdownToValidate;
          }
 
          if (FieldType == FieldTypes.DropdownList)
          {
              if (DropdownToValidate.SelectedIndex != -1)//Valid selection
              {
 
              }
              else
              {
                  e.ErrorText = "Le champ " + FieldToValidateControl.AccessibleDescription + " est obligatoire";
                  e.ErrorTitle = "";
                  e.IsValid = false;
              }
          }
          else if (FieldToValidateControl.Text.Length == 0 && IsRequired)//this automatically works for String Validation
          {
              e.ErrorText = "Le champ " + FieldToValidateControl.AccessibleDescription + " est obligatoire";
              e.ErrorTitle = "";
              e.IsValid = false;
          }
          else if (FieldToValidateControl.Text.Length == 0 && !IsRequired)
          {
          }
          else
          {
              switch (FieldType.ToString())
              {
                  case "Integer":
                      {
                          int numberInt;
                          if (!int.TryParse(FieldToValidateControl.Text, out numberInt))
                          {
                              e.ErrorText = "Le champ " + FieldToValidateControl.AccessibleDescription + " ne doit contenir que des entiers";
                              e.ErrorTitle = "";
                              e.IsValid = false;
                          }
                          else
                          {
                              if (int.Parse(FieldToValidateControl.Text) < 0)
                              {
                                  e.ErrorText = "Le champ " + FieldToValidateControl.AccessibleDescription + " doit être un entier positif";
                                  e.ErrorTitle = "";
                                  e.IsValid = false;
                              }
                          }
                          break;
                      }
                  case "Float":
                      {
                          float numberFloat;
                          if (!float.TryParse(FieldToValidateControl.Text, out numberFloat))
                          {
                              e.ErrorText = "Le champ " + FieldToValidateControl.AccessibleDescription + " ne doit contenir que des nombres";
                              e.ErrorTitle = "";
                              e.IsValid = false;
                          }
                          break;
                      }
                  case "String":
                      {
                          break;
                      }
              }
          }
      }

     

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Oct 2020, 09:39 AM

Hello, Fahmi,   

The ControlValidation event is specifically designed to handle cases in which the validation logic may not be easily defined with validation rules. It gives you the possibility to provide conditional validation and control the situations in which the control being validated has correct content or not. Additional information about the public API that RadValidationProvider offers is available here: https://docs.telerik.com/devtools/winforms/controls/validation-provider/validation-rules

There is a ValidationMode property that controls when the validation logic will be performed for the associated control. The available options are None, OnValidating, OnTextChange, Programmatically. The default value is OnValidating. It is up to you when to validate the control.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
ValidationProvider
Asked by
Alessio Bulleri
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Alessio Bulleri
Top achievements
Rank 1
Fahmi
Top achievements
Rank 1
Veteran
Share this question
or