0

I have a textbox called count. I want to enter only positive and negative numbers in the textbox using JavaScript or jQuery regular expression. Do not allow "text" and "decimals" and "special characters" in the textbox. Even copy and paste also it should take only positive or negative numbers.

Important:

  1. If I copy and paste text or decimal or special character also it should not take. It should take only positive or negative numbers.

  2. I want to enter and paste negative values without decimal.

  3. Arrow keys should work.

  4. Shift, Ctrl, Home, End, Enter like this buttons also should work.

Please check the following fiddle and please modify this.

Fiddle

 $(".allownumericwithoutdecimal").on("keypress keyup blur",function (event) {    
           $(this).val($(this).val().replace(/[^\d].+/, ""));
            if ((event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }
        });
   
 <span>Int</span>
<input type="text" name="numeric" class='allownumericwithoutdecimal'>
<div>Numeric values only allowed  (Without Decimal Point) </div>  

I am new for regex. Please help me for do this.

4
  • Is + sign allowed for positive numbers ? Commented Mar 11, 2016 at 12:34
  • not required. "-" symbol only required. Commented Mar 11, 2016 at 12:34
  • and "-" symbol should come only one time at the beginning. Not more than one. Commented Mar 11, 2016 at 12:38
  • Code doesnt work as expected Commented May 20, 2016 at 22:08

2 Answers 2

1

The regex you are looking for is this:

Regex: ^-?[0-9]+$

Explanation:

  • It checks for optional -ve sign. And only allows digits.

  • I have used [0-9] instead of \d because later one means numbers in different languages.

Regex101 Demo

PS: Am not a Javascript programmer so if anyone want to add fiddle or use this regex, go ahead.

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

6 Comments

Sorry noob. Please see my points what I have mentioned. Even copy paste also it should not take. And should allow arrow keys. Can you please add this example in jsfiddle?
@sathishkumar: Well yea this is not the complete solution. It's only the regex part. Good luck with the fiddle as am not a Javascript programmer.
This regex is good to use on the blur event, as a final validation. Actually, a regex is basic here, the problem is the code. See my fiddle adapted a bit from my former post. No idea if it works the way you need.
@WiktorStribiżew: Thanks. I think your fiddle is what OP requires. You should post it as answer.
@noob: No, I do not think it is working well. I cannot paste 12345 into the field.
|
1

This is also achievable without regex using Javascript keypress and charCode.

<input type="number" (keypress)="keypress($event, $event.target.value)" >

keypress(evt, value){

    if (evt.charCode >= 48 && evt.charCode <= 57 || (value=="" && evt.charCode == 45))       
    {  
        return true;
    }
    return false;
}

The given code won't allow user to enter alphabets nor decimal on runtime, just positive and negative integer values.

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.