1

I have the following code: http://jsfiddle.net/ntywf/1987/

$(document).ready(function () {    
    $('input').keyup(function() {
        var $th = $(this);
        $th.val($th.val().replace(/[-]/g, function(str) { 
            //alert('You typed " ' + str + ' ".\n\nPlease use only letters and numbers.'); 
            return ''; 
        }));
    });
});

what I want is to remove the "-" sign off when it is inserted. what happens is that the cursor is always the last decimal home. I just want this code not to let the user enter negative numbers. How can I do this? (the problem is to move the cursor within the input, since it is always sent to the last character)

Pete
  • 57,112
  • 28
  • 117
  • 166
pc_oc
  • 535
  • 3
  • 8
  • 26
  • Your code seems to work, in that it stops the user typing in a `-`, is there a specific issue you have with it? Note that you also want to perform this check on the `change` or `blur` events, as I can still copy the character in using the mouse. – Rory McCrossan Jun 29 '15 at 08:35
  • could not understand the problem – Mehmet Eren Yener Jun 29 '15 at 08:36
  • the problem is to move the cursor within the input – pc_oc Jun 29 '15 at 08:39
  • From what I understand, you don't want to allow negative numbers. You are removing dashes from the input. You are performing validation on the fly. Perhaps you might want to look at your flow and validate on submission? – tmutton Jun 29 '15 at 08:42
  • @tmutton yes, but my problem is that the cursor is always passed to the end, if you want to walk with the cursor left can not. – pc_oc Jun 29 '15 at 08:44
  • possible duplicate of [Validate that a string is a positive integer](http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer) – Pete Jun 29 '15 at 08:49
  • There is no way the "-" key is blocked when inserted? – pc_oc Jun 29 '15 at 09:25

4 Answers4

1

You can use a KeyCode (Link) to verify what key you pressed, and use replace to remove it:

$('input').keyup(function(e) {
        
    var code = e.keyCode || e.which;
    if(code == 109 || code == 189) { //Enter keycode
       //Do something
        var valor = $(this).val();
        $(this).val(valor.replace(/[-]/g, ''))
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text"/>
CesarMiguel
  • 3,756
  • 4
  • 24
  • 34
0

Use type = "numeric" and min="0" This way you can prevent your text-field from accepting alphabets as well. min=0 will always make sure that it will never accept -ve value.

<input type="number" min="0"/>

JSFIDDLE DEMO will be helpful to you.

Pralhad Narsinh Sonar
  • 1,406
  • 1
  • 14
  • 23
  • this does not solve my problem, since the customer to insert negative numbers manually. – pc_oc Jun 29 '15 at 08:47
  • I have tried it in the fiddle link and it is working. Its not allowing the `-` negative sign at all - even its not allowing the non-numeric values. What exact behavior you are looking at. If you prepare a step-by-step **USE CASE** - that will help a lot. – Pralhad Narsinh Sonar Jun 29 '15 at 09:46
0

Here what I have tried.

JS

$('input').keyup(function() {
        var $th = $(this).val();
        $th = $th.replace(/[-]/g, "");
        $(this).val($th)
        console.log( $(this).val());
    });

It will remove - sign from data.

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
0

This should solve your problem

What I have done is:

  • I have used the inbuilt HTML input field method setSelectionRange(), which sets the start and end positions of the current text selection in an element. (From MDN)

MDN Reference : https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange

JS Code:

$(document).ready(function () {    
  $('input').keyup(function() {
    var $th = $(this);
    $th.val( $th.val().replace(/[-]/g, function(str) { 
        //alert('You typed " ' + str + ' ".\n\nPlease use only letters and numbers.'); 
        return ''; 
    } ) );
    $('input')[0].setSelectionRange(0, 0); //this method sets the range to zero text starting from 0 index to 0 index
  });
});

JSFiddle: http://jsfiddle.net/dreamweiver/ntywf/1998/

dreamweiver
  • 6,002
  • 2
  • 24
  • 39