Creating and validating RadTextBox with code all generated dynamically

3 posts, 1 answers
  1. Wendy
    Wendy avatar
    2 posts
    Member since:
    Mar 2013

    Posted 05 Sep 2013 Link to this post

    Firstly let me apologise if this is such a simple issue but I am only just starting using Telerik controls.   All of the controls we are making have to be server controls and I am having trouble finding sufficient documentation. I Cannot make these as a web control and that is all the examples I can find.
    I have to make an address block as a server control using telerik radtextbox.  This block has Address 1/2/3 City and Postcode and I need to validate what the user enters into the Postcode box.
    I have added the text boxes with no problems - they work fine.

    private RadTextBox _txtAddress1 = new RadTextBox();

    private RadTextBox _txtAddress2 = new RadTextBox();

    private RadTextBox _txtAddress3 = new RadTextBox();

    private RadTextBox _txtCity = new RadTextBox(); 

    private RadTextBox _txtPostcode = new RadTextBox();

    (All have corresponding Public properties)

    Once I start trying to work out How to validate the _txtPostCode I run into issues.

    I thought I would declare  _txtPostcode.RegularExpressionValidator  but this is not a valid request.
    I then declared a new RegularExpressionValidator. 

    _regularExpressionValidator.ID = "txtValidator";

    _regularExpressionValidator.ErrorMessage = "Please enter valid Postcode";

    _regularExpressionValidator.ValidationExpression ="^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2}))$";

    _regularExpressionValidator.ControlToValidate = "_txtPostcode";

    setup the TextChanged and validation options of the _txtPostcode

    _txtPostcode.TextChanged += new EventHandler(_txtPostcode_TextChanged);

    _txtPostcode.AutoPostBack = true;

    _txtPostcode.CausesValidation = true;

    But I now cannot get to the point that the text box is validated. 
    Any help would be greatly received - code examples would be fantastic - I find that once someone has shown me a full example once I can then amend this as I need for future requirements as well
     

    Many Thanks
    Wendy

     

  2. Answer
    Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 09 Sep 2013 Link to this post

    Hi Wendy,

    I have tried this code and it is working for me:
    protected void Page_Load(object sender, EventArgs e)
    {
        RadTextBox textbox = new RadTextBox();
        Page.Form.Controls.Add(textbox);
     
        textbox.ID = "TextBox1";
        textbox.AutoPostBack = true;
        textbox.CausesValidation = true;
     
        RegularExpressionValidator validator = new RegularExpressionValidator();
        Page.Form.Controls.Add(validator);
        validator.ID = "txtValidator";
        validator.Text = "Please enter valid Postcode";
        validator.ValidationExpression = "^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2}))$";
        validator.ControlToValidate = textbox.UniqueID;
    }

    When I enter text like "AA11 1AA" it passes the validation and "InvalidZip" does not pass.

    Regards,
    Vasil
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Wendy
    Wendy avatar
    2 posts
    Member since:
    Mar 2013

    Posted 12 Sep 2013 Link to this post

    Thanks Vasil - beginner's mistake in trying to make things too complicated and forgetting that the validation is automatic and doesn't need and event changed placed on it.

Back to Top