简体   繁体   中英

RegularExpressionValidator VS Ajax 1.0.20229

We have a website that runs .NET Framework 2.0 with Ajax version 10618 .

But as it is, that is an old version of the dll, so we planned on switching it to the "newest" version of it for the .NET Framework 2.0 , the AjaxControlToolkit version 20229 .

On our tests, we detected a problem with the ASP control RegularExpressionValidator , which used to work fine with the old version.

Whenever the input to the target control doesn't match the validation, the control displays my text, which in this case is a red asterisc dispositioned, like, in the next row, and it displays the following in the control: "-1.7976931348623157e+308" .

Theres nothing wrong with the expression because as I said, it works fine with the older version of Ajax , and I couldn't find anything related to RegularExpressionValidators and Ajax versions.

PS: Both the validator and the control are inside an UpdatePanel.

PS 2: With the older version, it would put a 0 in the control and then show me the red asterisc right beside it when the expression wouldn't match.

Here's the control, fully copied:

<asp:RegularExpressionValidator ID="ValidateFooOrder" 
runat="server" ControlToValidate="txtFooNum"                                                    
Text="*" ErrorMessage="Invalid Foo number" 
ValidationExpression="^\d{0,4}$" ValidationGroup="GenerateFooFile" />

And it also has a NumericUpAndDownExtender attached to it:

<ajaxToolkit:NumericUpDownExtender ID="NumericExtenderFooNum" 
runat="server" TargetControlID="txtFooNum"                                                    
TargetButtonDownID="FooBack" TargetButtonUpID="FooForward" />

Ok, I've got the same issue and here are my findings:

First is the source of -1.7976931348623157E+308 . It is equal to the Minimum property of AjaxControlToolkit.NumericUpDownBehavior which is called in one of the Sys.Application.init Event handlers:

Sys.Application.add_init(function() {
    $create(AjaxControlToolkit.NumericUpDownBehavior, {"Maximum":1.7976931348623157E+308,"Minimum":-1.7976931348623157E+308, /* other non relevant stuff */);
});

So, no magic here, just a minimum value of Double . Minimum is the new property comparing to version 10618.

Next, why it is showed as soon as page is displayed? This happens because inside readValue function which is defined in AjaxControlToolkit.NumericUpDownBehavior.prototype a value this._min (which is equal to Minimum parameter from $create ) is assigned to input if it is empty. readValue sources:

readValue : function() {
        /// <summary>
        /// Parse value of textbox and this._currentValue to be that value.
        /// this._currentValue = this._min if some there is an exception
        /// when attempting to parse.
        /// Parse int or string element of RefValues
        /// </summary>

        if (this._elementTextBox) {
            var v = this._elementTextBox.value;
// The _currentValue of NumericUpDown is calculated here 
            // if textbox empty this._currentValue = this._min
            if(!this._refValuesValue) {
                if(!v) {
                    this._currentValue = this._min;
                } else {
                    try {
                        this._currentValue = parseFloat(v);
                    } catch(ex) {
                        this._currentValue = this._min;
                    }
                }
                if(isNaN(this._currentValue)) {
                    this._currentValue = this._min;
                }
// And assigned here. In case of empty input we will get -1.7976931348623157E+308 if Minimum was not changed
                this.setCurrentToTextBox(this._currentValue);
                this._valuePrecision = this._computePrecision(this._currentValue);
            } else {
                if(!v) {
                    this._currentValue = 0;
                } else {
                    var find = 0;
                    for (var i = 0; i < this._refValuesValue.length; i++) {
                        if (v.toLowerCase() == this._refValuesValue[i].toLowerCase()) {
                            find = i;
                        }
                    }
                    this._currentValue = find;
                }
                this.setCurrentToTextBox(this._refValuesValue[this._currentValue]);
            }
        }
    }

Before Minimum , in version 10618, default value was 0 . So I think described issue could be solved by specifying Minimum value explicitly in extender declaration:

<ajaxToolkit:NumericUpDownExtender ID="NumericExtenderFooNum" runat="server" 
     Minimum="0"
     TargetControlID="txtFooNum"
     TargetButtonDownID="FooBack" TargetButtonUpID

Another thing that I have found is that change event dispatching works wrong in new versions of IE (to make it work a Compatibility View should be enabled but I think this is not an option for public web sites).

The issue is in setCurrentToTextBox function. event object is always null in handlers (validation handlers for example) if created using document.createEvent . To fix this issue, condition should be swapped, so all events in IE will be created using createEventObject .

// Current implementation of version 20229
setCurrentToTextBox : function(value) {
            // full sources are not shown, only if matters here

            if (document.createEvent) {
                // event is created using createEvent
            } else if( document.createEventObject ) {
                // event is created using createEventObject 
            }
        }
    }

// Updated implementation
setCurrentToTextBox : function(value) {
            // full sources are not shown, only if matters here

            if (document.createEventObject) {
                // event is created using createEventObject 
            } else if(document.createEvent) {
                // event is created using createEvent
            }
        }
    }

Looking at the error message, implies the number being tested is outside the boundaries of the expression. The only allowed numbers are between 0 and 9999. No letters, punctuation, or other characters are allowed. The other part of the expression asserts anchors around the number, either as beginning and end of a line or word. The only other allowed value is NULL, which is probably where the update is being more strict and giving the error. For example, how do you validate a number or digit "\\d" that does not exist (zero characters)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM