-1

I want to limit a textarea field to Caps (A-Z), numbers (0-9) and this symbold (&). Is this possible with jQuery?

this is my js:

$('#textarea').keyup(function(e){
    var keyed = $(this).val().replace(/[\n]/g,'<br />');
    $('#output').html(keyed);
});

Any help please? thanks

Adrian
  • 491
  • 6
  • 23
  • 1
    Yes, it is possible. Use Regular Expressions and provide your code. – patrykf Sep 15 '14 at 08:56
  • possible duplicate of [how do i block or restrict special characters from input fields with jquery?](http://stackoverflow.com/questions/895659/how-do-i-block-or-restrict-special-characters-from-input-fields-with-jquery) – patrick Sep 15 '14 at 08:57
  • works only with inputs, i need for textarea too – Adrian Sep 15 '14 at 09:18

1 Answers1

0

Strictly speaking, I'd rather inform the user his input is not correct at validation time (form submition?) than just blocking it on keypress.

A function like this should work:

var test = function(chars){
  return /^([A-Z]|&|[0-9])*$/.test(chars);
}
$("input").change(function(){
  if(!test($(this).val()){
    //Do what you want
  }
});

textareas should work the same way.

sdumetz
  • 552
  • 2
  • 11