开发者

CustomValidator with server side validation only

开发者 https://www.devze.com 2023-01-06 19:02 出处:网络
I have a following problem: There are many validators on Page, all of them besides one have both client side and server side validation. One of them has only server side validation.

I have a following problem:

There are many validators on Page, all of them besides one have both client side and server side validation. One of them has only server side validation.

The problem:

My Page is being posted even some of client side validation are invalid. I think it should first validate client side and when everything is ok then it should check for server side.

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;
        }

asp.net part:

                <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="../Im开发者_开发百科ages/btn-verzenden.png" AlternateText="Verzenden" CssClass="verzendenBtn" OnClick="Submit_ImageButton_Click" runat="server" /> 

After some further test i've found that if there is even only first of them - UploadFoto_FileUpload_CustomValidator1 there is the same scenario:

when upload filed is empty then Ext error is displayed and page is not being posted

when file is chosen but ext is wrong then Ext error is displayed and page is not being posted

when file with correct ext is chosen then error message is not displayed but form is being posted even there are other validators with errors


I believe validation will fire all the validators - so that you don't get the annoying user experience of teasing out the next error/required field.

Assuming you can't write a meaningful client side script, just fake it and return that it's valid on the client side. Your server side logic will check the actual rule, but having client code will prevent it from posting back if some other valydator fails.


Sorry guys after whole day working on this i figure out that was a combination of two things:

  1. strange js behavior ext = ext.toLowerCase(); should be ext = ext.toString().toLowerCase(); because match return this as object type not string (as i supposed) so toLowerCase could not be use at this object .
  2. the second thing was missing dumb (witch sets isvalid to true) client side validation function for other customvalidators - thanks to @Mark Brackett

Sorry for bother you guys!


This is how validation works, if you can post back the page without some client side validators firing then the button or link or whatever fires the postback is either not in the validation group of the validators that are not firing or it has CausesValidation set to false.


You didn't set a validation group


It should only post back when all client side validators return true, have you confirmed this is the case using alert(args.IsValid);?

As an aside, you can use a RegularExpressionValidator to do file extension validation cleanly:

<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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号