Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:14:36+00:00 2026-06-09T12:14:36+00:00

You can use a regular RequiredValidator for i.e. a textbox but not for a

  • 0

You can use a regular RequiredValidator for i.e. a textbox but not for a label, so I’ve added a custom validator and use JQuery function to validate it. All works fine but I would like to be able to unit test it.

What changes would I need to do to, to test this function:

function LabelRequired_Validate(sender, args) {
    if (sender == $("#ctl00_cphMain_Agent2_Agent_Agent_LabelValidator1")[0]) {
        if ($("#ctl00_cphMain_Agent2_Agent_Agent_ValueLabel")[0].innerText.length > 0) {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    } else {
        args.IsValid = true;
    }
}

with this QUnit test (or similar):

test("Servicing_Topup_Branch_label_is_valid", function() {
    var span = $("span[id$=ValueLabel]");
    $(span).text(10 + " characters long");

    var args = { IsValid: true, Value: "" };
    LabelRequired_Validate($(span), args)

    ok(args.IsValid == true, "Validation Passed!");
});

Here is my custom validator:

<asp:CustomValidator ID="LabelValidator1" runat="server" ErrorMessage="<%$Resources:Main,RequiredFieldWarning %>"
        Display="Dynamic" ClientValidationFunction="LabelRequired_Validate" EnableViewState="False"
        class="alertMsg">
    </asp:CustomValidator>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:14:38+00:00Added an answer on June 9, 2026 at 12:14 pm

    Made a few changes to both the target and test functions. Here’s how it works.

    To the target:

    function LabelRequired_Validate(sender, args) {
        if (sender == $("span[id$=LabelValidator1]")[0]) {
            if ($("span[id$=ValueLabel]")[0].innerText.length > 0) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        } else {
            args.IsValid = true;
        }
    } 
    

    And to the test:

    test("Servicing_Topup_Branch_label_required_is_valid", function() {
        expect(2);
    
        var span1 = $("span[id$=LabelValidator1]");
        var span2 = $("span[id$=ValueLabel]");
    
        $(span2).text(18 + " characters long");
        var args = { IsValid: true, Value: "" };
        LabelRequired_Validate(span1[0], args)
    
        ok(args.IsValid == true, "Not empty label returns Valid - Passed!");
    
    
        $(span2).text("");
        args = { IsValid: true, Value: "" };
        LabelRequired_Validate(span1[0], args)
    
        ok(args.IsValid == false, "Empty label returns NotValid - Passed!");
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What regular expression can I use (if any) to validate that a given string
I know that for regular path I can use the MAX_PATH constant to create
I'm trying to find a single regular expression that I can use to parse
I can use .button() to create beautiful submit buttons, but the rest of the
I would like to validate the textbox for specific text and it must not
Can we use regular expressions to find out if a string follows these rules?
I need to validate serial numbers. For this we use regular expressions in C#,
In C/C++, you can use the regular gethostbyname() call to turn a dotted-IP address
How can I use regular expressions to change this string ':: 1:62 2:31 ::
I'm wondering if I can use jQuery to recode HTML that's entered into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.