How
to use CustomValidator in Asp.Net
The CustomValidator control allows you to write a
method to handle the validation of the value entered
Property /Description ...................
Property /Description ...................
1.ControlToValidate The
id of the control to validate
2.Display
The display behavior
for the validation control. Legal values are:
- None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
- Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
- Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
3.EnableClientScript A Boolean value that specifies whether
client-side validation is enabled or not
4.Enabled A Boolean value that specifies whether the
validation control is enabled or not
5.ErrorMessage The text to display in the ValidationSummary
control when validation fails. Note: This text will also be displayed in
the validation control if the Text property is not set
6.BackColor background color of the CustomValidator
control
7.ForeColor The foreground color of the control
8.Runat Specifies that the control is a server control.
Must be set to "server"
9.Text The message to display when validation fails
10.CssClass
The message to display with cssclass property
11.Id: A unique id for the control
12.IsValid A Boolean value that indicates whether the
control specified by ControlToValidate is determined to be valid
13.OnServerValidate
Specifies
the name of the server-side validation script function to be executed
For
example :
Code for Default.aspx…………………….
<tr><td>Name</td><td>
<asp:Textbox id="text1"
runat="server"
text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2"
runat="server"
ControlToValidate =
"text1"
ErrorMessage="Please enter Even No" onservervalidate="CustomValidator2_ServerValidate" >
</asp:CustomValidator>
</td>
</tr>
Code for Default.aspx.cs…………………….
protected void
CustomValidator2_ServerValidate(object source, ServerValidateEventArgs e)
{
//if (e.Value.Length == 8)
// e.IsValid = true;
//else
// e.IsValid = false;
int aa =Convert.ToInt32(e.Value);
if (aa % 2 == 0)
e.IsValid
= true;
else
e.IsValid
= false;
}
14.ClientValidationFunction Specifies the name of the client-side validation script function to be executed.
Note: The script must be
in a language that the browser supports, such as VBScript or JScript
With
VBScript, the function must be in the form:
Sub FunctionName (source, arguments)
With
JScript, the function must be in the form:
function FunctionName (source, arguments)
For Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<%--<script type="text/javascript">
function validateLength(oSrc, args) {
args.IsValid = (args.Value.length >= 8);
}
</script>--%>
<script type="text/javascript">
function validateLength(oSrc, args)
{
var v=args.Value;
args.IsValid
= (v %2==0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Textbox id="text1"
runat="server"
text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2"
runat="server"
ControlToValidate =
"text1"
ErrorMessage = "You must enter even no "
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
<asp:Button ID="Button1"
runat="server"
Text="Button"
/>
</div>
</form>
</body>
</html>
Result:
0 comments:
Post a Comment