Reputation: 95
Here are my lines of code:
For the Textbox
and the CustomValidator
:
<asp:TextBox ID="Edit_Tbox_Email" runat="server" placeholder="Email Address..." CausesValidation="true" ValidationGroup="submission"></asp:TextBox>
<asp:CustomValidator runat="server" ControlToValidate="Edit_Tbox_Email" ID="cv_Email" OnServerValidate="cv_Email_ServerValidate" ErrorMessage="!" data-toggle="tooltip" title="Invalid Input!" ValidationGroup="submission"></asp:CustomValidator>
This is for the Button
to allow the user to change his/her email:
<asp:Button ID="Edit_But_Email" runat="server" Text="Change" CssClass="btn btn-small" OnClick="Edit_But_Email_Click" ValidationGroup="submission" CausesValidation="true"/>
On server side:
protected void Edit_But_Email_Click(object sender, EventArgs e)
{
if(Page.IsValid)
Edit_Lab_Email.Text = Edit_Tbox_Email.Text;
}
protected void cv_Email_ServerValidate(object source, ServerValidateEventArgs args)
{
string emailRegex = "^\\w+[\\w-\\.]*\\@\\w+((-\\w+)|(\\w*))\\.[a-z]{2,3}$";
if (Edit_Tbox_Email.Text.Length == 0 || Regex.IsMatch(Edit_Tbox_Email.Text, emailRegex))
{
cv_Email.IsValid = false;
}
}
But the problem here is that cv_Email_ServerValidate()
doesn't even fire. Basically, it doesn't validate the Textbox
. Also, I don't want to use the RequiredFieldValidator
and RegularExpressionValidator
. I want to combine their functionalities into just one validator. And as much as possible I would like to use the code behind(c#) only and not by jQuery. Thank You!
Upvotes: 0
Views: 796
Reputation: 334
Your button have a ValidationGroup
but your CustomValidator
don't have one. If you want your button to valide it, you must set the same ValidationGroup
.
Edit:
You have to set ValidateEmptyText="true"
otherwise, it won't validate empty value.
Upvotes: 1