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

Email Validation In RadTexbox Inside the Grid

1 Answer 288 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Princia
Top achievements
Rank 1
Princia asked on 22 Feb 2012, 04:38 PM
Hi All,

I am uisng a RadTexbox for taking Email Address from user,which is inside the Radgrid's EditTemplate.I have used asp RegExpressionValidator for validating the user input.For inserting of new record in the grid the RegExpressionValidator is fired, if user enters the invalid email ID.Problem here is  that ,once  I save the record in grid   (by giving correct Email Address), then try to modify the Email Address the RegExpressionValidator is not fired for the invalid input.In other words I can put the problem like,for initial insert of email address RegExpressionValidator is fired for invalid input .If I try to modify Email address with invlaid data RegExpressionValidator  is not at all fired.I want client side validation for this problem.Please help me out in this regard.



Thanks& Regards
Princia

1 Answer, 1 is accepted

Sort by
0
Nekud
Top achievements
Rank 2
answered on 08 Apr 2014, 09:40 PM
Hey Princia,

You can use the following client side validation for this.

function ValidateEmail(field) {// field is the textbox.
            if (!checkEmail(field.value.replace(/ /g, ''))) {
                    alert("Invalid Email: " + emailArray[i]);
                    field.focus();
                }
            }
        }
        function checkEmail(email){
            var regex = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
            return regex.test(email);
        }

On your server side all you need to do is:
protected void radGridAlerts_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                (e.Item.FindControl("txtEmail") as TextBox).Attributes.Add("onblur", "ValidateEmail(this)");
}
}

This should validate the email once the textbox loses focus.

Thanks,
Nekud
Tags
General Discussions
Asked by
Princia
Top achievements
Rank 1
Answers by
Nekud
Top achievements
Rank 2
Share this question
or