add client-side validation to all the radtextboxes in my application

5 posts, 0 answers
  1. Ananthalvan
    Ananthalvan avatar
    8 posts
    Member since:
    Feb 2012

    Posted 06 Jul 2012 Link to this post

    Hi,
      I have multi-line radTextBox used all over my application. I want to associate a generic validation across all these multi-line radTextBoxes. To accomplish that, I thought of using jQuery to find control of type textarea (found this through firebug) and called my validation function. All this is fine. I'm able to trigger validation. One part where I didn't succeed is in my validation function, I'm using args which is not getting passed when I trigger it through jQuery. Is there a way to get around this problem?

    To give you code example, in the below code, args is not defined when I try to add validation dynamically
    $('textarea').keypress(function (sender, args) {
                ValidateSpecialCharcters(sender, args);
            });

            function ValidateSpecialCharcters(sender, args) {
               if(validation condition fails)
                    args.set_cancel(true);
                }
            }

    any ideas?

    ~/Ananth
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Jul 2012 Link to this post

    Hi Ananthalvan,

    Try the following code to achieve your scenario.

    JS:
    $('textarea').bind('keypress', function (e)
     {
      if(validation condition fails)
      {
       return false; // Cancel the event
      }
     });

    Hope this helps.

    Thanks,
    Shinu.
  3. Ananthalvan
    Ananthalvan avatar
    8 posts
    Member since:
    Feb 2012

    Posted 06 Jul 2012 Link to this post

    Hi Shinu,
      i don't see it working. I receive same error message "args is Undefined". From your code, when I pass in e as args argument for my validation method, I'm not able to get hold of the key pressed.


    ~/Ananth
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Jul 2012 Link to this post

    Hi Ananth,

    Here is the code that I tried based on your scenario. You can get hold of the key pressed using the parameter args.

    JS:
    <script type="text/javascript">
    $('textarea').bind('keypress', function (args)
     {
      var ret = validate(args);
      return ret;
     });
    function validate(args)
     {
      var keycode = args.keyCode;
      if(validation fails)
      {
       return false;
      }
     }
    </script>

    Hope this helps.

    Thanks,
    Shinu.
  5. Ananthalvan
    Ananthalvan avatar
    8 posts
    Member since:
    Feb 2012

    Posted 11 Jul 2012 Link to this post

    That worked Shinu! I made one change to the script. args.keyCode didn't work with me. I used below instead

    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;

    Thank you.

    ~/Ananth
Back to Top