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

How to set a regular expression on a textbox?

1 Answer 512 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 16 Aug 2017, 11:13 AM

I have a RadGrid that I populate from my SQL database.  I use the RadGrid1_ItemCreated event to set the textbox width presently and that works fine.  Now I want to set a regular expression for the same textbox.  It is a textbox that contains an email address.  Here is my C# code:

protected void gvEmailRedirectMaint_ItemCreated(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
            {
                string myMsg;
                GridEditableItem edititem = (GridEditableItem)e.Item;
                Regex regex = new Regex(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
                TextBox txtbx = (TextBox)edititem["RedirectEmailAddress"].Controls[0];
                txtbx.Width = Unit.Pixel(300);
                Match match = regex.Match(txtbx.Text);

                if(match.Success)
                    myMsg = txtbx.Text + " is Valid";
                else
                    myMsg = txtbx.Text + " is Invalid";
            }
        }

When I run this, it performs no editing.  If I click the update button, it saves the email address even though it is not in the proper format.  What am I doing wrong?

1 Answer, 1 is accepted

Sort by
0
Bob
Top achievements
Rank 1
answered on 16 Aug 2017, 11:26 AM
Some more clarification, I want to use the RegEx when the user clicks the Edit icon.  I use the same regular expression when a new row is added and that works fine.  But I need to validate that this is a valid email address when the user tries to change the email address as well.  
Tags
Ajax
Asked by
Bob
Top achievements
Rank 1
Answers by
Bob
Top achievements
Rank 1
Share this question
or