A couple of days ago, I faced a situation whereby I needed to add some form validation to an aspx page.

I had options of either JavaScript or ASP.net client side validation. JavaScript seemed like an easier solution…
<h5>Client-side JavaScript for form validation</h5>

<br />
&lt;script language=&quot;javascript&quot;&gt;<br />
&lt;!--<br />
Function CheckForm(form)<br />
{<br />
 for(var intCtr = 0; intCtr &lt;= (form.elements.length - 5); ++intCtr)<br />
 {<br />
 var temp = form.elements[intCtr];<br />
 if(temp.type == &quot;text&quot; &amp;&amp; temp.value == &quot;&quot;)<br />
 {<br />
 alert(&quot;Please Enter All Information!&quot;);<br />
 temp.focus();<br />
 return false;<br />
 }<br />
 }<br />
 return true;<br />
}<br />
//--&gt;<br />
&lt;/script&gt;<br />

All good and well with the above, however the code above doesn’t check for all the information that you might need on the form you are building. This piece of code determines only whether the user entered anything at all in all five fields within the form. It does not determine whether the user entered an actual e-mail address within the e-mail address text box, whether the user entered a number between two given numbers, or whether the password and the confirm password text boxes match. After awhile, you can see that you need many JavaScript functions to obtain the level of form validation required.
<h5>ASP.net Client-side validation</h5>

<br />
&lt;%@ Page Language=&quot;C#&quot; %&gt;<br />
&lt;script runat=&quot;server&quot;&gt;</p>
<p>void Button1_Click(Object sender, EventArgs e) {<br />
 Label1.Text = &quot;Page is valid!&quot;;<br />
<p>&lt;/script&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
 &lt;form runat=&quot;server&quot;&gt;<br />
 &lt;p&gt;<br />
 &lt;asp:TextBox id=&quot;TextBox1&quot;<br />
 runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br />
 &amp;nbsp;<br />
 &lt;asp:RequiredFieldValidator<br />
 id=&quot;RequiredFieldValidator1&quot; runat=&quot;server&quot;<br />
 ErrorMessage=&quot;Required!&quot;<br />
 ControlToValidate=&quot;TextBox1&quot;&gt;<br />
 &lt;/asp:RequiredFieldValidator&gt;<br />
 &lt;/p&gt;<br />
 &lt;p&gt;<br />
 &lt;asp:Button id=&quot;Button1&quot; onclick=&quot;Button1_Click&quot;<br />
 runat=&quot;server&quot; Text=&quot;Button&quot;&gt;&lt;/asp:Button&gt;<br />
 &lt;/p&gt;<br />
 &lt;p&gt;<br />
 &lt;asp:Label id=&quot;Label1&quot; runat=&quot;server&quot;&gt;&lt;/asp:Label&gt;<br />
 &lt;/p&gt;<br />
 &lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />

<strong>Causing the RequiredFieldValidator server control to fire</strong>

There are a few things to be aware of when using the RequiredFieldValidator server control in your ASP.NET pages. First of all, the most important property of this validation control is the ControlToValidate property. The value assigned to this control needs to be the value of the id property for the control to which you want to link the RequiredFieldValidator control. You can link only one RequiredFieldValidator server control to any other server control on the page. For instance, if you have three required TextBox controls on your ASP.NET page, you need three separate RequiredFieldValidator controls to make sure that they are all required.


The second property of this control is the ErrorMessage property. This is the text that appears where the RequiredFieldValidator control is located on the ASP.NET page if the TextBox is left empty. Instead of using the ErrorMessage property, you can also use the Text property:

<br />
&lt;asp:RequiredFieldValidator id=&quot;RequiredFieldValidator1&quot;<br />
 runat=&quot;server&quot; Text=&quot;Required!&quot;<br />
 ControlToValidate=&quot;TextBox1&quot;&gt;<br />
&lt;/asp:RequiredFieldValidator&gt;<br />

Presently, six different validation server controls are available for ASP.NET:


The “RequiredFieldValidator” was the common one used in my situation and turned out to be pretty helpful.