How to prevent a semicolon from being entered into html text input, but allowing a colon?

George Chen

I want to allow input such as 1:10, but not 1;10. However, : and ; both correspond to keyCode 186, so using keyCode to prevent the ; key from inputting into my input field does not work. I also researched into using charCodes, but charCodes don't have the ; or : values. Finally, I looked at ascii tables. They have semicolon and colon values. Is there any way for me to possibly use ascii tables to prevent the ; key from inputting into my textbox, but the : key to be allowed? Or is there another approach that will let me do this? I also thought about detecting two key inputs in a row, so that I could detect a shift key input, but that seems like a dirty solution.

 $("input.form-1").bind({
    keydown: function(e) {
        if(e.which ===186) { //trying to disallow semicolons, which also disallows colons
            return false;
        }
    }
});
Sven Writes Code

Like Rory said, you should be using on. Instead of checking the shiftKey, you can also just check the key property on the event. MDN KeyboardEvent.key

$("input.form-1").on({
    keydown: function(e) {
        if(e.key === ";") { // disallow semicolon
            return false;
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="form-1" type="text" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTML / CSS: Prevent background color from being overwritten when text entered into a form

.stopPropagation() placed on input not allowing for input text to be entered

Where can in insert mysql_real_escape_string in here? And how to prevent html from being entered?

How do I prevent a leading space from being entered in a textbox?

How to prevent char from being entered into JTextField after dispatching event?

How can I prevent input controls from allowing events to bubble?

How to prevent an input from being disabled?

ng tags input not allowing same text twice to be entered in the text box

Regular expression prevent dash from being entered

How to block every number from being entered in text field

How do I prevent placeholder text from being vertically centered in a large height input form element?

Is there a fix to prevent text input in paint from being very aliased/blurry?

How to prevent ’ character being entered into a textbox?

Prevent html input type=number from ever being empty

How to prevent part of HTML text from being copied when copying adjacent?

Prevent decimal place being entered into input field on mobile

How to prevent a text from overflowing AND from being clipped?

How to get the height of entered text in a JTextArea while it is being entered by the user?

How can I prevent a matTooltip text from being truncated?

How to prevent Suggested Actions from being converted to text?

How to prevent text from being cut off in swiftUI

WPF: Prevent RowDetailTemplate from being entered using down arrow key?

SwiftUI TextField prevent certain characters from being entered?

Angular- How to prevent text in input from showing if input is invalid

How to prevent files from being stolen by editing html code?

How to prevent HTML elements from being pushed down the page

Change the colour of this input text depending on the value being entered

Check if text is being entered in input using Javascript/jQuery

How to prevent reading input from text field if text field is disabled