2

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?

Programmer7
  • 159
  • 1
  • 4
  • 10
  • i dont think so you need to do it the WPF way. The already present validators are enough for it as far as webpages are concerned. – Manish Sep 18 '16 at 16:39
  • So the RequiredFieldValidator, RangeValidator, etc is considered Server-side Validation for ASP.NET? I already know how to do Client-side Validation on html pages with JavaScript or jQuery, but ASP.NET validations seems too easy when compared to WPF or Windows Forms Validation. – Programmer7 Sep 18 '16 at 16:43
  • Yea you can say that it works that way. Ans yes it's easy than WPF and also jquery and HTML5 validation is much easier to implement and provides wide range of validations too... – Manish Sep 18 '16 at 16:57

4 Answers4

5

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.

Gaurav P
  • 1,097
  • 1
  • 14
  • 19
  • Thanks for the info. But if user has Javascript disabled will my way still do server side validation? – Programmer7 Sep 18 '16 at 18:42
  • 1
    Yes, server side validation will work, only the client side javascript will not work. If you are using JQuery to perform AJAX call to web service then that will also not work. Thus most of modern site like google etc doesn't allow to continue if JS is disabled. – Gaurav P Sep 19 '16 at 03:50
0

When using the ASP.Net validation server-controls, you still want to double-check that that Page.IsValid before attempting to save the data on the server-side.

dperish
  • 1,493
  • 16
  • 27
  • How do I do that? By putting if (IsValid) and then whatever code is needed to process the application inside of it? – Programmer7 Sep 18 '16 at 16:45
  • This is explained pretty well here: http://stackoverflow.com/questions/13762467/how-does-page-isvalid-work – dperish Sep 18 '16 at 16:54
0

According to MSDN, those controls inject some kind of javascript validation at client-side;

By default, the validator controls inject client-side ECMAScript (JavaScript) into the page to perform validation checking in the browser. This gives users instant feedback on validation errors; without the client script, checking for validation errors would require a round trip to the server, which could be slow at times. In fact, you cannot submit the page until the client-side validation succeeds

https://msdn.microsoft.com/en-us/library/a0z2h4sw.aspx

Some controls also have a server validation option, which you can use if you need to:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args){
try 
{ 
    DateTime.ParseExact(args.Value, "m/d/yyyy", 
        System.Globalization.DateTimeFormatInfo.InvariantInfo); 
    args.IsValid = true; 
} 
catch 
{ 
   args.IsValid = false; 
}}

You can also check for Page.IsValid

Ygor Henrique
  • 53
  • 1
  • 8
0

ASP.NET Validation controls can be used in both client and server-side validation. Validation controls always perform validation on the server. They also have complete client-side implementation. Use the EnableClientScript property to specify whether client-side validation is enabled.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55