Reputation: 151
I want to restrict html input, so user cannot input char # into the input
i tried to use pattern but it seems not working
i add these pattern to my input pattern="[^-#]+"
i expect the user cannot input char #, but it still can, what was wrong?
Upvotes: 0
Views: 69
Reputation: 2984
use replace
function
to replace # with space on keyup
or oninput
event.
<input type="text"
oninput="this.value = this.value.replace(/#/g, '')" />
Upvotes: 0
Reputation: 3305
Use a key up event and use replace()
to replace #
with empty string ''
.
<script>
function validateInput()
{
var data = document.getElementById("input").value;
data = data.replace('#','');
document.getElementById("input").value = data;
}
</script>
<input type="text" id="input" onkeyup="validateInput()" />
Upvotes: 1
Reputation: 342
This worked for me.
Place your logic in onkeydown
or onkeypress
event handler function. find out the charCode
. It must be 35. Then simply event.preventDefault()
if the condition is met.
Upvotes: 0