Removing unwanted characters from a text field using jQuery

What I would like to get is how to remove specific characters from a text field (or text field) using jQuery. I have code in C #, but I cannot translate it into jQuery javascript. My problem is that I don’t know how to get the value from the text field as an array of characters, which I can then scroll through and compare with the given set of unwanted characters. This is how far I came to jQuery:

$("input[type=text], textarea").change(function() {

   // code here

});

This is my code in C #:

for (int i = 0; i < charArray.Length; i++)
{
    current = charArray[i];
    if ((current == 0x9) ||

        (current == 0xA) ||

        (current == 0xD) ||

        ((current >= 0x20) && (current <= 0xD7FF)) ||

        ((current >= 0xE000) && (current <= 0xFFFD)))
        _validXML.Append(current);
}

return _validXML.ToString().TrimEnd((char)32, (char)160) ;

UPDATE:

I went with a combination of some answers below (I will promote them), and my last jQuery looks like this:

$(document).ready(function() {
    $(":text, textarea").change(function() {
        var text = "";
        var arr = $(this).val()
        $.each(arr, function(i) {
            var c = arr.charCodeAt(i);
            if ((c == 0x9) ||
                (c == 0xA) ||
                (c == 0xD) ||
                (c >= 0x20 && c <= 0xD7FF) ||
                (c >= 0xE000 && c <= 0xFFFD)) 
            {
                text += arr.charAt(i);
            }
        });
        $(this).val(text);
    });
});

Thanks everyone!

+3
source share
6 answers

Textarea:

<textarea id="item" name="item" rows="5" cols="80">Some text in here</textarea>

JQuery Code:

var text = $('#item').val();
var newtext = "";
for (var i = 0; i < text.length; i++) {
   var c = text.charCodeAt(i);
   if ((c == 0x9) || (c == 0xA) || (c == 0xD) || 
       (c >= 0x20 && c <= 0xD7FF) ||
       (c >= 0xE000 && c <= 0xFFFD)) {
       newtext += c;
   }
}
$('#item').val(newtext);

jQuery, .

+1

, :

$("input[@type='text'], textarea").change(function() {
    this.value = this.value.replace(/[^\w\d]+/gim,"");
});
+6

charCodeAt() length .

- :

$("input[type=text], textarea").change(function() {
  var text = $(this).val()

  for(var i = 0; i < text.length; ++i) {
    var currentChar = text.charCodeAt(i);

    // Do something with it...
});

charAt(), , Unicode, charCodeAt().

+1

(onkeydown/onkeypress/onkeyup) /textarea, , , .

$("input[type=text], textarea").observe('keypress', function(e) {
 var keynum; 
 if(window.event)
 {
  keynum = e.keyCode
 }
 else if(e.which)
 {
  keynum = e.which
 }
 if(keynum == '13' || keynum == 'something else' || [...])
 {
  Event.stop(e);
 }
});
-1

textarea try:

$('input[type=textarea]').change(function(){
   var value = $(this).val(); 
   ...........
});

, . jquery (jQuery.grep())

var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];
$("div").text(arr.join(", "));

arr = jQuery.grep(arr, function(n, i){
  return (n != 5 && i > 4);
});
$("p").text(arr.join(", "));

arr = jQuery.grep(arr, function (a) { return a != 9; });
$("span").text(arr.join(", "));
-1

I prefer that the character not be entered first, using this type of javascript function (from my shady past):

each input control has something like this: OnKeyPress = 'checkKey (this, "a-Za-z0-9", "N", "10");'

the function looks like this:

   //****************************************************************************
   // Function: checkKey()
   //   Author: Ron Savage
   //     Date: 10-11-2004 
   //    
   // Description: This function tests reg exp syntax.
   //****************************************************************************
   function checkKey(textControl, reExpr, allCaps, maxlen)
    {
      popupMessage.hide();

      keyStr = String.fromCharCode(event.keyCode);
      textLength = textControl.value.length;

      if (allCaps == 'Y')
         {
         keyStr = keyStr.toUpperCase();
         event.keyCode = keyStr.charCodeAt(0);
         }

      if ( reExpr != '' )
         {
        reString = '[^' + reExpr + ']';
         re = new RegExp(reString, 'g');

         //alert('RE: ' + reString);

         result = keyStr.match(re);

         if (result)
            {
            beep();
            event.returnValue = false;
            showPopupMessage(textControl, result.toString() + ' not allowed!');
            }
         }

      if ( textLength > maxlen )
         {
            beep();
            event.returnValue = false;
            showPopupMessage(textControl, 'Max length [' + maxlen + '] exceeded!');
         }

      //alert('Key: ' + keyStr + ' code: ' + event.keyCode);
      }
-1
source

Source: https://habr.com/ru/post/1711276/


All Articles