anothershrubery
anothershrubery

Reputation: 21023

Adding a user control containing javascript inside it breaks jquery-ui

I have a user control defined as:

<div>
    <asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="validator" runat="server" ControlToValidate="txtBox"></asp:CustomValidator>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= txtBox.ClientID %>').blur(function (event) {
            $('#<%= txtBox.ClientID %>').removeClass('errorTextBoxServer');
            testBox($('#<%= txtBox.ClientID %>'), <%= RegularExpression %>);
        });

        testBox($('#<%= txtBox.ClientID %>'), <%= RegularExpression %>);
    });

    function <%= txtBox.ClientID %>_validateBox(source, args)
    {
        $('#<%= txtBox.ClientID %>').removeClass('errorTextBoxServer');
        args.IsValid = testBox($('#<%= txtBox.ClientID %>'), <%= RegularExpression %>);
    }
</script>

testBox is defined as:

function testBox(thisBox, RegularExpression) {
    if ($(thisBox).val() != "") {
        var regex = new RegExp(RegularExpression);
        var value = $(thisBox).val();
        if (!regex.test(value)) {
            //event.preventDefault();
            $(thisBox).addClass('errorTextBox');
            return false;
        }
        else {
            $(thisBox).removeClass('errorTextBox');
            return true;
        }
    }
    else {
        $(thisBox).removeClass('errorTextBox');
        return true;
    }
}

And it is placed in a web page as follows:

<asp:TextBox ID="txtD1DOB" runat="server" readonly="true"></asp:TextBox>
<wpm:ValidatedTextBox id="txtUpdatedBalance" runat="server" RegularExpression="/^\d{1,3}(,?\d{3})*(\.\d{2,4})?$/" />

Using the following js:

$(document).ready(function () {
    var $j = jQuery.noConflict();
    $j("input[id*='txtD1DOB']").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "dd/mm/yy",
    });
});

When I run the app js throws the error:

Object doesn't support property or method 'datepicker'

If I remove the user control <wpm:ValidatedTextBox id="txtUpdatedBalance" runat="server" RegularExpression="/^\d{1,3}(,?\d{3})*(\.\d{2,4})?$/" /> then the datepicker works as expected.

I have checked if there are other versions of jquery-ui loading but can't find anything and can't seem to find what would be causing the issue. Any ideas?

EDIT: I tried changing the user control to simply:

<asp:TextBox ID="txtUpdatedBalance" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="txtUpdatedBalance_RegularExpressionValidator" runat="server" ErrorMessage="Invalid value" ControlToValidate="txtUpdatedBalance" ValidationExpression="/^\d{1,3}(,?\d{3})*(\.\d{2,4})?$/"></asp:RegularExpressionValidator>

But the same thing occurs. Removing the RegularExpressionValidator allows everything to work.

EDIT 2: In response to Frebin Francis in the comments the entire head section is:

<head runat="server">
    <meta charset="utf-8" />
    <title><%: Page.Title %> - My ASP.NET Application</title>
    <asp:PlaceHolder runat="server">     
          <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>  
    <webopt:BundleReference runat="server" Path="~/Content/css" /> 
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
    <script src="Scripts/wpm_portal.js"></script>
    <meta name="viewport" content="width=device-width" />
    <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>

It also has a ScriptManager defined as:

<asp:ScriptManager runat="server">
    <Scripts>
        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
        <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
        <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
        <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
        <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
        <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
        <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
        <asp:ScriptReference Name="WebFormsBundle" />
    </Scripts>
</asp:ScriptManager>

Upvotes: 3

Views: 776

Answers (2)

Michael Liu
Michael Liu

Reputation: 55529

ASP.NET 4.5 introduced unobtrusive JavaScript for client-side validation. When this feature is enabled (which it is by default), validation controls like CustomValidator automatically include a copy of jQuery from your project's Scripts directory. This copy conflicts with the version of jQuery you're referencing in the <head>.

The easiest way to resolve the problem is to turn off unobtrusive validation, which you can do by adding the following setting to Web.config:

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>

If instead you want to use unobtrusive validation, then follow these steps:

  1. Using the NuGet Package Manager, upgrade AspNet.ScriptManager.jQuery to version 2.1.3 and install AspNet.ScriptManager.jQuery.UI.Combined version 1.11.3.
  2. Remove the <script> tags that include jquery.min.js and jquery-ui.min.js from the Google CDN.
  3. Add <asp:ScriptReference> tags for "jquery" and "jquery.ui.combined" to the <asp:ScriptManager> control.

Upvotes: 1

eyegropram
eyegropram

Reputation: 672

Isn't RegularExpression a namespace in .NET, change the variable name to rX or something else.

For compatibility:

Try using the noConflict object outside of the document.ready

Such as:

var $j = jQuery.noConflict();
$j(document).ready(function () {

    $j("input[id*='txtD1DOB']").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "dd/mm/yy",
    });
});

Do the same for all jQuery related calls.

Upvotes: 0

Related Questions