C# – ASP.NET Client-side vs Server-side Validation

asp.netcvalidationwebforms

I am constantly getting incorrect info from the "Instructor" at school on this.

For ASP.NET Web Applications, the Validators from the Toolbox like CompareValidator, RangeValidator, RequiredFieldValidator, etc, are those considered Server-Side Validation?

Because we also add a jQuery NuGet package and it gives real time validation when the user tabs, like if the user types a letter when a number is required.

For WPF's in C# I create a Validator Class or use a Library and check validation that way through Static Methods. Should I be doing it this way in ASP.NET as well? Or are the RequiredFieldValidators, etc enough for Server-Side Validation?

Here is an example:

<div class="form-group">
    <label class="control-label col-sm-4">Length:</label>
    <div class="col-sm-4">
        <asp:TextBox ID="txtLength" runat="server" CssClass="form-control" MaxLength="15"></asp:TextBox>
    </div>
    <div class="col-sm-4">
        <asp:RequiredFieldValidator ID="rfvLength" runat="server" ErrorMessage="Length is required" 
            ControlToValidate="txtLength" CssClass="error" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
        <asp:RangeValidator ID="rngLength" runat="server" ErrorMessage="Must be between .01 and 10,000" 
            MaximumValue="10000" MinimumValue=".01" ControlToValidate="txtLength" CssClass="error" Display="Dynamic" 
            SetFocusOnError="True" Type="Double"></asp:RangeValidator>
    </div>                            
</div>

thanks

******EDIT*****

Guys you are giving unclear or incomplete answers just like my "Instructor" does.

Yes or No please, is this Server-side Validation in ASP.NET?

<div class="form-group">
    <label class="control-label col-sm-4">Length:</label>
    <div class="col-sm-4">
        <asp:TextBox ID="txtLength" runat="server" CssClass="form-control" MaxLength="15"></asp:TextBox>
    </div>
    <div class="col-sm-4">
        <asp:RequiredFieldValidator ID="rfvLength" runat="server" ErrorMessage="Length is required" 
            ControlToValidate="txtLength" CssClass="error" Display="Dynamic" SetFocusOnError="True"></asp:RequiredFieldValidator>
        <asp:RangeValidator ID="rngLength" runat="server" ErrorMessage="Must be between .01 and 10,000" 
            MaximumValue="10000" MinimumValue=".01" ControlToValidate="txtLength" CssClass="error" Display="Dynamic" 
            SetFocusOnError="True" Type="Double"></asp:RangeValidator>
    </div>                            
</div>

and then I also add:

protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                double length = Convert.ToDouble(txtLength.Text);
                double width = Convert.ToDouble(txtWidth.Text);

                Rectangle r = new Rectangle(length, width);

                txtArea.Text = r.Area().ToString("f");
                txtPerimeter.Text = r.Perimeter().ToString("f");
                txtDiagonal.Text = r.Diagonal().ToString("f");
            }               
        }

Is this correct as Server-side Validation in ASP.NET and am I using the if (IsValid) part correctly?

Best Answer

You are mixing ASP.NET Validation Control and difference between Client side and Server Side validation.

When ASP.NET page renders the Validation Controls RequiredFieldValidator and RangeValidator etc., it automatically creates Client side JavaScript function to perform validation within the browser i.e. client side validation.

IsValid is server side validation feature of ASP.Net Pages by which it verifies whether the Asp.Net Validation Control used in the WebForm/ WebPage had performed the validation, and at the server side code, if all the validation is applied (i.e. if all mandatory fields etc. are entered ) then IsValid becomes true.

Note that is not mandatory to put IsValid in server side code, as the JavaScript functions which are created will do the required validations on the client side.

So, basically ASP.NET Validation Control helps to validate ASP.NET pages from both Client Side and Server Side, and have an advantage compared to plain JavaScript validation.

Another example is JQuery code (JavaScript Library) which can be used to do Client Side validation like validating if textbox's value is empty or not. At the same time JQuery can be used to do server side validation by an AJAX call to a web service method on the server side.