Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom validator

Custom validator

Scheduled Pinned Locked Moved ASP.NET
help
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    diacoboss
    wrote on last edited by
    #1

    Hi guys, I have a small problem, hope u could help me ! I'm tryin to use a custom validator on a Textbox ! Look : * The code behind now protected void first_CV_ServerValidate(object source, ServerValidateEventArgs args) { if (TextBox2.Text == "") { args.IsValid = false; } else { args.IsValid = true; } } Nothing changes if I set the ValidateEmptyText property of the CustomValidatorCustomValidator to true. Basically, I hoped to see a message when the Textbox is empty! But nothin happen when Click on my submit button Please help me

    M C 2 Replies Last reply
    0
    • D diacoboss

      Hi guys, I have a small problem, hope u could help me ! I'm tryin to use a custom validator on a Textbox ! Look : * The code behind now protected void first_CV_ServerValidate(object source, ServerValidateEventArgs args) { if (TextBox2.Text == "") { args.IsValid = false; } else { args.IsValid = true; } } Nothing changes if I set the ValidateEmptyText property of the CustomValidatorCustomValidator to true. Basically, I hoped to see a message when the Textbox is empty! But nothin happen when Click on my submit button Please help me

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      Why not use the RequiredFieldValidator [^]instead.

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      D 1 Reply Last reply
      0
      • M Manas Bhardwaj

        Why not use the RequiredFieldValidator [^]instead.

        Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

        D Offline
        D Offline
        diacoboss
        wrote on last edited by
        #3

        REquiredFieldValidator is not appropriated cuze I try use the control on a TextBox with a visible mode is False in this case the RequiredFieldValidator doesn't work. Help me about the custom validator, I think it's the only solution.

        C 1 Reply Last reply
        0
        • D diacoboss

          REquiredFieldValidator is not appropriated cuze I try use the control on a TextBox with a visible mode is False in this case the RequiredFieldValidator doesn't work. Help me about the custom validator, I think it's the only solution.

          C Offline
          C Offline
          Coding C
          wrote on last edited by
          #4

          Why do you want a custom validator even? You can just check the value of textbox before your processing begins and if its empty just display error message (whatever you want). For example on button click event you can check the value of textbox and avoid further action if its empty. HTH

          Coding C# ExciteTemplate

          1 Reply Last reply
          0
          • D diacoboss

            Hi guys, I have a small problem, hope u could help me ! I'm tryin to use a custom validator on a Textbox ! Look : * The code behind now protected void first_CV_ServerValidate(object source, ServerValidateEventArgs args) { if (TextBox2.Text == "") { args.IsValid = false; } else { args.IsValid = true; } } Nothing changes if I set the ValidateEmptyText property of the CustomValidatorCustomValidator to true. Basically, I hoped to see a message when the Textbox is empty! But nothin happen when Click on my submit button Please help me

            C Offline
            C Offline
            compninja25
            wrote on last edited by
            #5

            I've run into this issue before as well where I had a textbox hidden and the requiredfield validator would mess the form up. I'm not sure what it is exactly you are attempting to pull off, but I've found it easier in some cases to just build the controls instead of hiding. For example, if you are hiding a textbox until someone clicks a button and then showing it, try this instead and then attach the required field validator to it....

            TextBox tb1 = new TextBox();
            RequiredFieldValidator re1 = new RequiredFieldValidator();
            re1.ControltoValidate = tb1.ID;
            divControl.Controls.add(tb1);

            The above code needs some tweaking..but should give you the idea. The validator won't screw up the form because the textbox doesn't even exist until you place it as a new control. :) That, or I've personally become quite fond of using javascript to validate forms...

            var namebox = document.getElementById('<%= Name_textbox.ClientID %>');
            if (namebox.value == ''){
            var message = "We seem to be missing some information. Please correct the following items before submitting:\n\n";
            if (namebox.value == '') {
            message += "Name box cannot be blank.\n";
            namebox.style.backgroundColor = 'yellow';
            }

            hopefully this helps! good luck

            Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

            D 1 Reply Last reply
            0
            • C compninja25

              I've run into this issue before as well where I had a textbox hidden and the requiredfield validator would mess the form up. I'm not sure what it is exactly you are attempting to pull off, but I've found it easier in some cases to just build the controls instead of hiding. For example, if you are hiding a textbox until someone clicks a button and then showing it, try this instead and then attach the required field validator to it....

              TextBox tb1 = new TextBox();
              RequiredFieldValidator re1 = new RequiredFieldValidator();
              re1.ControltoValidate = tb1.ID;
              divControl.Controls.add(tb1);

              The above code needs some tweaking..but should give you the idea. The validator won't screw up the form because the textbox doesn't even exist until you place it as a new control. :) That, or I've personally become quite fond of using javascript to validate forms...

              var namebox = document.getElementById('<%= Name_textbox.ClientID %>');
              if (namebox.value == ''){
              var message = "We seem to be missing some information. Please correct the following items before submitting:\n\n";
              if (namebox.value == '') {
              message += "Name box cannot be blank.\n";
              namebox.style.backgroundColor = 'yellow';
              }

              hopefully this helps! good luck

              Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

              D Offline
              D Offline
              diacoboss
              wrote on last edited by
              #6

              Thanks compninja25 for helping, Can u even tell me which language U R using here, is dat ...?

              C 1 Reply Last reply
              0
              • D diacoboss

                Thanks compninja25 for helping, Can u even tell me which language U R using here, is dat ...?

                C Offline
                C Offline
                compninja25
                wrote on last edited by
                #7

                Oh sorry. The top part is c#, and the bottom code block is javascript.

                Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups