I had been created text box custom validation control and I’m using this in my aspx page and its working great up to finding length means if it is empty means it’ll validate correctly
but now wanted to enhance this, like if I wanted to enter my email id then first condition is, it should not be empty, second is it should of the format abc@abc.com.
and if it is empty means I need to show error msg like ‘please enter email id!!’ and if it is not in above format then show ‘please enter correct email id!!’.
here is my code,
TextboxCustomValidation.aspx
namespace TextboxCustomValidator
{
public class TextboxCustomValidation : BaseValidator
{
protected override void OnPreRender(EventArgs e)
{
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "CheckTextboxvalid");
this.CreateJavaScript();
}
base.OnPreRender(e);
}
protected void CreateJavaScript()
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<script type=""text/javascript"">function CheckTextboxvalid(ctrl){");
sb.Append(@"var txtlength = document.getElementById(document.getElementById(ctrl.id).controltovalidate);");
sb.Append(@"if(txtlength.value.length>2){");
sb.Append(@"return true; }else");
sb.Append(@" {return false;} ");
sb.Append(@"}</script>");
Page.ClientScript.RegisterClientScriptBlock(GetType(), "JSScript", sb.ToString());
}
protected override bool ControlPropertiesValid()
{
TextBox txt = FindControl(ControlToValidate) as TextBox;
return (txt != null);
}
protected override bool EvaluateIsValid()
{
return CheckTextboxIsValidorNot();
}
protected bool CheckTextboxIsValidorNot()
{
TextBox txtControlName = (TextBox)FindControl(this.ControlToValidate);
if (txtControlName.Text.Length > 0)
return true;
else
return false;
}
}
}
**MyTextBoxValidation.aspx**
<%@ Register TagPrefix="TCV" Namespace="TextboxCustomValidator" %>
<body>
<form id="form1" runat="server">
<div>
UserName :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<TCV:TextboxCustomValidation ControlToValidate="txtUserName" ErrorMessage="Please enter username !!"
EnableClientScript="true" runat="server"></TCV:TextboxCustomValidation>
<asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
<TCV:TextboxCustomValidation ControlToValidate="txtEmailId" ErrorMessage="please enter txtEmailId!!" EnableClientScript="false"
runat="server">
</TCV:TextboxCustomValidation>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save_Click" />
</div>
</form>
</body>
</html>
help me,
any reply means will be appreciated.
Should work like this