-2

I can't find how I could remove all lines on textarea containing letters, as I enter it.

For removing it automatically I use bind change and keyup.

I want textarea to show only numbers and spaces, no matter what's entered to it.

ENTERED text and numbers shouldn't change and should contain the same line breaks and spaces. Only LINES containing text should be removed.

Simon
  • 1,314
  • 4
  • 14
  • 26
  • Possible duplicate of [Javascript Regex to limit Text Field to only Numbers (Must allow non-printable keys)](http://stackoverflow.com/questions/19093143/javascript-regex-to-limit-text-field-to-only-numbers-must-allow-non-printable-k) – Ivijan Stefan Stipić Feb 07 '16 at 12:38
  • It's similar, but it doesn't solves the problem. Scripts on link you've provided, removes all characters except numbers and leaves numbers only. I need to remove only lines containing text and leave all the line breaks and spaces for numbers as it was. – Simon Feb 07 '16 at 12:49
  • 1
    Do you have a question? ("I want" doesn't count). – georg Feb 07 '16 at 12:55
  • "I want" explains what I am asking for. – Simon Feb 07 '16 at 13:05
  • 1
    Such wishes are usually sent to Santa Claus. A requirement list is not a question ... – Teemu Feb 07 '16 at 13:11
  • God. What's wrong with you guys? I just asked how to do this, as a ton of people here. What the hell – Simon Feb 07 '16 at 13:16
  • 1
    They are mean that SO is not code request forum. You need to start by yourself, then if you got stuck, you can ask and include your code and the specific problem.. – Mosh Feu Feb 07 '16 at 13:17
  • I got stuck, that's why I am asking what I want. – Simon Feb 07 '16 at 13:21
  • I can't I don't know how to do it, so it would work as I want. – Simon Feb 07 '16 at 13:27
  • Why don't you just post the code you obviously have already. Then explain what it does instead of what you want (that's clear by now). That way you would introduce a specific problem you're facing. – Teemu Feb 07 '16 at 13:34
  • Look link what I put there. You need just save in string your data from textarea, clean with regex and return back into textarea. There are complete wxplanation, just read and change setup. – Ivijan Stefan Stipić Feb 07 '16 at 13:45

2 Answers2

0

Use

<textarea onkeypress="checkval(this);"></textarea>

<script>

function checkval(a){ var b=/[^0-9]/gi;

if(a.value.search(b)>-1){

a.value=a.value.replace(b,""); } } </script>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
0

I found the answer:

It can be done using regex:

this.value = this.value.replace(/[^0-9 \n]/g, '');
this.value = this.value.replace(/^\s*\n/gm, '');

First one for numbers and spaces, to leave. And second one for line breaks. It will change everything except these.

Example:

$('textarea').bind( "keyup", function() {
this.value = this.value.replace(/[^0-9 \n]/g, '');
this.value = this.value.replace(/^\s*\n/gm, '');
})
Simon
  • 1,314
  • 4
  • 14
  • 26