0
votes

I encountered an error using Regular Expression Validator.

1) Create an ASP.NET Empty application (as WebApplication) 2) Add a Web Form in the WebApplication 3) Add the following line:

inside the <head></head>

4) Create a folder "Script", still inside the WebApplication tree 5) Add the jquery-1.8.2.js file inside the folder 6) Inside the div below the form add two textbox:

<asp:TextBox ID="A" name="A" type="text" size="50" MaxLength="100" runat="server" />
    <asp:TextBox ID="B" name="B" type="text" size="50" MaxLength="100" runat="server" />

7) For each textbox add a RegularExpressionValidator, so the code would be:

<asp:TextBox ID="A" name="A" type="text" size="50" MaxLength="100" runat="server" />
    <asp:RegularExpressionValidator 
                ValidationGroup="alpha" ID="ControlA"
                runat="server" ErrorMessage="*" ControlToValidate="A" 
                ValidationExpression=".{10}">
            </asp:RegularExpressionValidator>
    <asp:TextBox ID="B" name="B" type="text" size="50" MaxLength="100" runat="server" />        
    <asp:RegularExpressionValidator 
                ValidationGroup="beta" ID="ControlB"
                runat="server" ErrorMessage="*" ControlToValidate="B" 
                ValidationExpression=".{2}">

8) create a button below the text boxes:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

9) In the button method "Button_click" create a string to check if it works: protected void Button1_Click(object sender, EventArgs e) { string text = A.Text + " " + B.Text; }

10) run the program.

11) any error like: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

My version of VS is: 12.0.30501.00 Update 2. .NET Framework: 4.5.51641 I tried with IE and Chrome, with jquery-1.8.2.js and jquery-2.1.1.js Windows 8.1 Enterprise.

2

2 Answers

2
votes

Remove this line in your web.config :

<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

Or add this line on your web.config :

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

Or in your global.asax :

ValidationSettings.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None
0
votes

ASP.NET 4.5 and later has unobtrusive validation which controls the client side validation. In your web.config <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

means the unobtrusive validation is on and will use jQuery...which you can add by: Right clicking solution explore->Manage NuGet. Click on Browse and type "aspnet.scriptmanager.jquery"

or leave it off (the old way which uses script elements in HTML for validation via JavaScript) <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

The benefit of having it on is that it reduces the amount of JavaScript that has to be generated.