This is a migrated thread and some comments may be shown as answers.

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

4 Answers 94 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ananthalvan
Top achievements
Rank 1
Ananthalvan asked on 06 Jul 2012, 05:15 AM
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

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Jul 2012, 11:15 AM
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.
0
Ananthalvan
Top achievements
Rank 1
answered on 06 Jul 2012, 06:56 PM
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
0
Shinu
Top achievements
Rank 2
answered on 09 Jul 2012, 12:02 PM
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.
0
Ananthalvan
Top achievements
Rank 1
answered on 11 Jul 2012, 01:31 PM
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
Tags
General Discussions
Asked by
Ananthalvan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ananthalvan
Top achievements
Rank 1
Share this question
or