-2

I have a form where a min and a max have to be entered by user. What is the recommendation for validation for this case. Only server validation or client and server validation.

How do you validate on client both inputs? Do you know of an example that does this?

UPDATE

The validation I am talking about is the compare validation (all other validation I know how to do, there are a lot of examples on the internet). However I did not found a client validation for having two fields that are connected with compare.

Basically how do you do client validation for min that it has a value less than max and a validation for max that it has a value less than min. Or what is the recommended alternatives to this.

NEW UPDATE

It seems that the first phrase of my question has been lost. On a form I have two input fields lets say FieldA and FieldB. FieldA has to be less than FieldB and FieldB has to greater than FieldA.

My question refers to: Is there an example on how to do this kind of validation. Since the common response is to do validation on a client then my question would be what validation should I add to the two fields? Do I make both field not valid when the conditions are not met. If yes then how do I make both fields valid again after the user has changed one of the fields.

Dan
  • 683
  • 2
  • 8
  • 24
  • What technologies are you using for frontend and backend? – Creyke Sep 04 '18 at 10:50
  • BE: asp.net core c#. FE: jquery validation – Dan Sep 04 '18 at 10:50
  • Possible duplicate of [MVC Validation Lower/Higher than other value](https://stackoverflow.com/questions/18577777/mvc-validation-lower-higher-than-other-value) – Stefan Sep 05 '18 at 09:02
  • It is a duplication however I need the full solution. I have posted another question more explicit https://stackoverflow.com/questions/52223040/validate-a-field-inside-a-validation-function – Dan Sep 07 '18 at 12:56

1 Answers1

4

Both.

The reason:

  • client validation only: your server will be hacked.
  • server side only: your users will be annoyed, although it's better than client side only
  • both: happy users, not hack-able. A little bit of more work though.

As for the techniques, there are loads, depending on your setup. You could use DataAnnotations with validation attributes server side, and there are tons of jquery, angular, knockout etc. validation plugins for client side javascript.


Addition: As @bradbury9 states: server side validation "fairly easy"

An example of jQueries validation can be found here:

<script>
$(document).ready(function(){
    $("#commentForm").validate({
       rules: {
          name: "required",
          email: {
                   required: true,
                   email: true,
                 },
          comment: "required"
     }
});});
</script>

note: as stated before: client side validation is highly dependent on the technique you are using. For AngularJs there is e.g.: this library.


update

On your request, for a min max, aka range validation, an example with attributes (server-side):

//model
public class Foo
{
    [Range(1, 100)]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
} 

And the controller:

// POST: Movies/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Foo foo)
{
    if (!ModelState.IsValid)
    {
      //error
    }
}

And the javascript side:

$( "#commentForm").validate({
  rules: {
    field: {
      required: true,
      range: [13, 23]
    }
  }
});

See source for details.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • There should be pointed aout that server side validation [is fairly easy](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation?view=aspnetcore-2.1) – Cleptus Sep 04 '18 at 12:31
  • @bradbury9: I agree. I added it to the answer. – Stefan Sep 04 '18 at 12:41
  • I don't need the required validation. I was asking about the compare validation. If the min and less than max and the max is greater than min – Dan Sep 04 '18 at 13:46
  • @Dan: ah, yes, I have added a range validation example. – Stefan Sep 04 '18 at 14:06
  • @Stefan I think you did not understand the question. The user has to enter two fields: one for minim and one for maxim. I do not need to validate one field for min and maxim. There are already lots of examples for Range validation. But there is no example for validating two fields that are dependent with comparing to each other – Dan Sep 05 '18 at 08:56
  • 1
    Ah, that's more complicated. I once wrote a custom server side attribute to do just that. Here is an example, it's with date's but it can easily be adjusted: https://stackoverflow.com/questions/18577777/mvc-validation-lower-higher-than-other-value – Stefan Sep 05 '18 at 09:01
  • The link helps but I still think that if I add both validations at one point one the fields is valid and one is not valid. This is the reason for the question. How do you deal with this. – Dan Sep 05 '18 at 09:09
  • Hmm, since the question is almost a day old, maybe it's best to ask it again to regain full attention. Make sure you are explicit, we're not all native English speakers ;-) – Stefan Sep 05 '18 at 09:42