0

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?

5

3 Answers 3

1

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()" />

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

use replace function to replace # with space on keyup or oninput event.

<input type="text" 
oninput="this.value = this.value.replace(/#/g, '')" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.