Skip to content

Can anyone fix my js textarea restrict special characters space issue

Original code

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
function valid(f) {
!(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">

i prefer the code in javascript ! thanks

i can’t figuerout how to let the spaces to be allowed in ? anyone have an idea i tried in jquery but idk why im always getting errors same as js / i think the space bar RegExp is “” s “” but it doesn’t seem to work ?



You can just use a literal space character. That’s what I’m doing here after the 0-9:

function valid(f) {
  !(/^[A-zÑñ0-9 ]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9 ]/ig,''):null;

I created a before and after here:

As you can see in that example, the “after” allows the space, whereas the “before” (your code) replaces it.

So just add the space character and you should be good to go.

User contributions licensed under: CC BY-SA
1 People found this is helpful