## Allow numbers and minus sign only

input type=number allow negative

allow only numbers in textbox jquery using regex

allow only numbers and decimal in textbox angular 6

allow only numbers and one decimal point javascript

jquery allow only numbers and plus

allow only positive numbers in textbox jquery

pattern to allow only numbers and decimals

I'm trying to allow numbers and a minus sign only in a `input type="text"`

. I was able to allow numbers only, but I can't allow the minus sign. I tried the following regular expression to allow numbers only:

/\D/

When I add the minus sign, it doesn't seem to work:

/-?\D/

(The reason I have the question mark (?) is because there won't always be a minus sign.) When I try adding a minus sign to the textfield, it doesn't work. Here's the code:

(I tried adding a code snippet, but it wouldn't compile correctly.)

##### CodePen

<input id="num" type="text" /> var input = document.getElementById('num'); input.addEventListener("keypress", function(e) { var keycode = (e.keyCode ? e.keyCode : e.which); if (/-?\D/.test(String.fromCharCode(keycode))) { // a non–digit was entered e.preventDefault(); } });

I suggest using this regex to **match a valid number**:

/^[+-]?\d+$/

and having you logic reversed in `test`

method as this:

var input = document.getElementById('num'); input.addEventListener("change", function(e) { if (!/^[+-]?\d+$/.test(this.value)) { // a non–digit was entered console.log("non-digit was entered"); $('#num').val(''); } });

**Enter only numbers and minus sign in textbox,** keyCode = 0; alert("Please Enter Number Only"); return false; } } </script>. Now on top of this I should allow minus sign to be entered at the start. Hi, I would like to ask how can I validate a textbox so that it will onyl accept plus sign followed by numbers like this: +1234567890123 Thanks, Jassim number and plus sign only - jQuery Forum Loading

Try using `pattern`

attribute with `RegExp`

`[0-9-]+`

var input = document.querySelector("input"); input.oninput = function(e) { if (!this.checkValidity()) { // do stuff console.log("invalid input") } }

input:invalid + label[for="num"]:before { content: "invalid input"; }

<input id="num" type="text" pattern="[0-9-]+" minlength="1" placeholder="requires input" required /><label for="num"></label>

**[Solved] Enter only numbers and minus sign in textbox,** keyCode = 0; alert("Please Enter Number Only"); return false; } } </script> Now on top of this I should allow minus sign to be entered at the start. Re: Enter only numbers and minus sign in textbox Jun 02, 2015 05:57 PM | Rion Williams | LINK If you wanted to allow a minus sign '-' to be entered only as the first character, you could simply apply a condition to check for that :

I think this code allow you to only input numbers and minus sign. But because the code also allow "ctrl + c", "ctrl + v", "ctrl + a", etc.., The textfield can be input other characters by "ctrl + v"(paste).

If you want to block "ctrl + v"(paste) also, comment out the code below.

$("#num").on("keydown", function(e){ //character, not with ctrl, alt key if(e.keyCode >= 65 && e.keyCode <= 90 && !e.ctrlKey && !e.altKey) { return false; } // number with shift if(e.keyCode >= 48 && e.keyCode <= 57 && !!e.shiftKey) { return false; } // ` ~ - _ = + \ | [ { ] } ' " ; : / ? , < . > var otherKeys = [186, 187, 188, 189, 190, 191, 192, 219, 220, 221, 222]; if(otherKeys.indexOf(e.keyCode) !== -1) { // allow minus sign if(e.keyCode === 189 && !e.shiftKey) { return true; } return false; } // if you want to block "ctrl + v"(paste), comment the below code out //if(e.keyCode === 86 && !!e.ctrlKey) { // return false; //} return true; });

**Regex to allow numbers, plus symbol, minus symbol,** I need to get only those values in the column having numbers, plus symbol, minus symbol. Below are sample rows. 0-100000� In this example we will only allow numbers, the dash and the period to be entered. We will set the maximum number of characters entered to 15 with the <Run FontWeight="Bold">MaxLength</Run> property. We use the <Run FontWeight="Bold">PreviewTextInput</Run> of the TextBox to trap the keystrokes that the user types.

`\D`

refers to non-digits

You were probably thinking of `\d`

/^-?\d+$/

**number and plus sign only,** Hi, I would like to ask how can I validate a textbox so that it will onyl accept plus sign followed by numbers like this: +1234567890123 Thanks,� CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100

You are testing against the result of `String.fromCharCode(keycode)`

.

This can never be two characters.

if (! /-|\d/.test(String.fromCharCode(keycode))) { // a non–digit was entered

The regex `/-|\d/`

checks for exactly one character to be either either `-`

or a digit.

For use in your code example the result of the match is negated with `!`

.

As you are testing every single keypress, this will still allow the input to contain `-`

the middle of the value like `123-456-789`

.

**How to use Validation to only allow negative numbers?,** needs to enter the discount in a negative amount (i.e. "-400") for the be a POSITIVE number and just changing the final calculation if that's� Only allow negative numbers in Excel with Data Validation. Normally, the Data Validation function can help you, please do as follows: 1. Select the cells or column that you want to allow only negative numbers entered, and then click Data > Data Validation > Data Validation, see screenshot: 2.

**JavaScript : Checking for all numbers,** An integer with an optional leading plus(+) or minus(-) sign. To get a string contains only numbers (0-9) with a optional + or - sign (e.g. +1223,� 1. Allow "$" char at the front and TextBox only cantains one "$" at most. 2. Allow valid Decimal value (Positive or Negative) in TextBox or be at the behind of the "$" 3. Other invalid characters is not allowwed. However here are two changes: 1. Handle TextBoxCurrency_KeyDown event instead of TextBoxCurrency_KeyPress event. 2.

**How do I allow negative numbers with the ES_NUMBER edit control ,** How do I allow negative numbers with the ES_NUMBER edit control style? Raymond Chen How can we extend� ES_ NUMBER to accept the minus sign? The ES_ For bonus points, let's allow the minus sign only if i. Microsoft Excel displays negative numbers with a leading minus sign by default. It is good practice to make negative numbers easy to identify, and if you’re not content with this default, Excel provides a few different options for formatting negative numbers.

**Numbers and Optional Plus sign,** Click to open in Reference. \+ Escaped character. Matches a "+" character (char code 43). ? Quantifier. Match between 0 and 1 of the preceding token. \d Digit. this regular expression can not add "." at single time. he wants to add the "." at single time only. But i want only single dot in the textbox. Currently user can enter multiple dot.

##### Comments

`\D`

means anything*but*numbers. It doesn’t make sense to use it to allow*only*numbers.- You want this:
`/-?\d+/`

- I am laughing so hard, @Jessica. Nobody seems to get that you really meant \D, since your code
*disallows*everything what is*not a number*Literally nobody cared enough to read the line below, nor your //comment. - Use character class: /[0-9\-]/ for allow numbres and digits only and /[^0-9\-]/ for inverse
- @KiraCrafter The
`-`

character may not require escaping within class - I tried that, but I still can't insert the minus sign.
- I think you need to use
`blur`

in`addEventListener`

rather than`keypress`

- How will that help me be able to insert a minus sign?
- The code is still not good as you can enter the minus sign multiple times like
`-------78`

- No,
`^[+-]?\d+$`

will not allow multiple minus signs at start. - try your own demo and enter:
`123`

- @dev-null See updated post, added
`+`

following`\d`