In ASP.NET there exists a set of validation server control which are useful to validate the data on form. In this post we will see how to use RequiredFieldValidator.
RequiredFieldValidator is used to check if field on the form is not empty. In order to use RequiredFiledValidator, we need to add server control
RequiredFieldValidator example
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:<br />
<asp:TextBox runat="server" ID="Name" />
<asp:RequiredFieldValidator runat="server" ID="validateName" ControlToValidate="Name"
ErrorMessage="Enter the name." ForeColor="Red" />
<br />
<br />
<asp:Button runat="server" ID="btnSubmit" Text="Ok" OnClick="Post_Page" CausesValidation="" />
</div>
</form>
</body>
</html>
Here you will notice that the validation is happening at client side means without postback, this is because by default validator control does client side validation if browser supports DHTML. And if browser does not supports scripting then it will do server side validation, means page will postback and it will check for "Page.IsValid()" and then will return true or false.
And if we explicitly want to do the server side validation then we need to set EnableClientScript="false"