Custom validator with server-side validation only

I have the following problem:

There are many validators on the page, all of them, in addition, have checks both on the client side and on the server side. One of them has only server side validation.

Problem:

My page is published, although some client-side checks are not allowed. I think that first you need to check the client side, and when everything is in order, then it should be checked on the server side.

the code:

Js part:

var hash = { '.jpg' : 1, '.jpeg' : 1, '.bmp' : 1, '.png' : 1 }; function FileExtension(obj, args) { var file = '<%=UploadFoto_FileUpload.ClientID %>'; var re = /\..+$/i; var ext = $("#" + file).val().match(re); if (ext != undefined) { ext = ext.toLowerCase(); if (hash[ext]) { args.IsValid = true; } else { args.IsValid = false; } } else { args.IsValid = false; } } function Validator2(obj, args){ args.IsValid = true; } 

part of asp.net:

  <asp:CustomValidator ID="UploadFoto_FileUpload_CustomValidator1" ErrorMessage="Ext error" ClientValidationFunction="FileExtension" OnServerValidate="UploadFoto_FileUpload_CustomValidator1_ServerValidate" Display="Dynamic" runat="server" /> <asp:CustomValidator ID="UploadFoto_FileUpload_CustomValidator2" ErrorMessage="De foto is te groot (maximaal 6mb)" ClientValidationFunction="Validator2" OnServerValidate="UploadFoto_FileUpload_CustomValidator2_ServerValidate" Display="Dynamic" runat="server" /> <asp:FileUpload ID="UploadFoto_FileUpload" CssClass="uploadField" runat="server" /> <asp:ImageButton ID="Submit_ImageButton" ImageUrl="../Images/btn-verzenden.png" AlternateText="Verzenden" CssClass="verzendenBtn" OnClick="Submit_ImageButton_Click" runat="server" /> 

After some additional test, I found that if there is only the first of them - UploadFoto_FileUpload_CustomValidator1, then the same scenario:

when the download file is empty, then an error message is displayed and the page is not published

when the file is selected, but ext is incorrect, then an error message is displayed and the page is not published

when a file with the correct ext is selected, then the error message is not displayed, but the form is submitted, even there are other validators with errors

+4
source share
5 answers

I believe that checking will trigger all validators, so you wonโ€™t get an annoying user approach to teasing the next error / necessity field.

Assuming you cannot write a meaningful client side script, just fake it and return that it is valid on the client side. Your server-side logic will check the actual rule, but the presence of the client code will prevent it from being sent back if any other validator fails.

+1
source

Sorry, guys after a whole day working on this, I understand that this was a combination of two things:

  • strange behavior js ext = ext.toLowerCase(); should be ext = ext.toString().toLowerCase(); , because match returns this as an object type, not a string (as I expected), so toLowerCase cannot be used on this object.
  • The second thing was missing the dumb (witches set isvalid to true) client side validation function for other customvalidators - thanks to @Mark Brackett

Sorry to bother you guys!

+1
source

This is how validation works, if you can send the page back without any activating validators on the client side, then the button or link, or everything that works after the postback, is either not part of the validator validation group that doesnโ€™t shoot or has CausesValidation installed false.

0
source

You have not set up a validation group

0
source

It should only send messages when all client-side validators return true, did you confirm this using alert(args.IsValid); ?

Aside, you can use RegularExpressionValidator to authenticate the file extension:

 <asp:RegularExpressionValidator runat="server" ErrorMessage="Only JPEG, GIF, and PNG files are allowed!" ValidationExpression="^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$" ControlToValidate="UploadFoto_FileUpload">*</asp:RegularExpressionValidator> 
0
source

Source: https://habr.com/ru/post/1315501/


All Articles