ASP.NET Web Forms文本框验证器脚本文件无法加载求助
Hey there! Let's tackle your ASP.NET Web Forms validation issue step by step. You mentioned trying a third-party validation engine that wouldn't load, plus needing to validate if TextBox5's value is greater than TextBox6's—here's how to fix both:
First: Fix the Script Loading Failure
That third-party validation engine not loading is likely a path or compatibility issue. Let's sort that first:
Check your local script paths: If you've downloaded the validation engine files to your
Scriptsfolder, make sure your references use the correct relative path. For example:<script src="Scripts/jquery.validationEngine.js" type="text/javascript"></script> <script src="Scripts/jquery.validationEngine-en.js" type="text/javascript"></script>If you're using a master page or nested directory, use ASP.NET's path resolution to avoid errors:
<script src='<%= ResolveUrl("~/Scripts/jquery.validationEngine.js") %>' type="text/javascript"></script>Upgrade your jQuery version: jQuery 1.4.4 is extremely outdated (released in 2010!). It might not play nicely with modern validation scripts. Swap it for a stable, recent version like jQuery 3.7.1:
<script src="https://code.jquery.com/jquery-3.7.1.min.js" type="text/javascript"></script>
Better Alternative: Use ASP.NET's Built-in CustomValidator
Instead of relying on a third-party library that might cause loading headaches, ASP.NET Web Forms has a native solution for custom validation logic—CustomValidator. It’s more integrated with the Web Forms ecosystem and includes server-side validation (critical for security, since client-side validation can be bypassed).
Step 1: Add Controls to Your Page
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> <asp:CustomValidator ID="TextBoxComparisonValidator" runat="server" ControlToValidate="TextBox5" ErrorMessage="文本框5的值不能大于文本框6的值" ClientValidationFunction="validateTextBoxes" OnServerValidate="TextBoxComparisonValidator_ServerValidate" Display="Dynamic">*</asp:CustomValidator> <asp:Button ID="SubmitBtn" runat="server" Text="提交" OnClick="SubmitBtn_Click" />
Step 2: Client-Side Validation Script
Add this script to your page (inside <script> tags, after jQuery or as pure vanilla JS):
function validateTextBoxes(sender, args) { // Get values and convert to numbers const val5 = parseFloat(document.getElementById('<%= TextBox5.ClientID %>').value); const val6 = parseFloat(document.getElementById('<%= TextBox6.ClientID %>').value); // Validate: if val5 > val6, mark as invalid args.IsValid = !(!!val5 && !!val6 && val5 > val6); }
Step 3: Server-Side Validation (Mandatory!)
Add this code to your code-behind to prevent invalid submissions from bypassing client-side checks:
protected void SubmitBtn_Click(object sender, EventArgs e) { if (Page.IsValid) { // Your submission logic here (e.g., save to database) } } protected void TextBoxComparisonValidator_ServerValidate(object source, ServerValidateEventArgs args) { if (decimal.TryParse(TextBox5.Text, out decimal val5) && decimal.TryParse(TextBox6.Text, out decimal val6)) { args.IsValid = val5 <= val6; } else { // Mark as invalid if inputs aren't valid numbers args.IsValid = false; } }
Alternative: Pure jQuery Validation
If you prefer a client-side-only approach (though remember to add server-side checks too!), you can use jQuery directly:
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script> $(document).ready(function() { $('#<%= SubmitBtn.ClientID %>').on('click', function(e) { const val5 = parseFloat($('#<%= TextBox5.ClientID %>').val()); const val6 = parseFloat($('#<%= TextBox6.ClientID %>').val()); if (!isNaN(val5) && !isNaN(val6) && val5 > val6) { alert('文本框5的值不能大于文本框6的值'); e.preventDefault(); // Stop form submission } }); }); </script>
Final Notes
- Always pair client-side validation with server-side validation to ensure data integrity.
- If you stick with the third-party validation engine, double-check file permissions and that the script files aren't corrupted.
内容的提问来源于stack exchange,提问作者L.Johnson

