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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…