How to prevent negative and positive signs from entering into textboxes using js

How to prevent negative and positive signs from entering into textboxes using js

  

i am using a textbox field which data type field = "number"

it is taking - ve sign and + sign  in text boxes and any other special characters in like "." to stop entering into textbox . please help me 

i am using the following jquery on keypress event of extended property 

SyntaxEditor Code Snippet

"$('#" + var2.Id + "').keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A, Command+A
            (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) || 
             // Allow: home, end, left, right, down, up
            (e.keyCode >= 35 && e.keyCode <= 40)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });"

please tell me where i am going wrong where var2 is name of the textbox 


Hi,

I tried an exact copy of your code in an "onkeypress" event and I'm not able to type the + or - sign.

You can also try the following code that I have used:

"var ex=/[\-\+]/; if (ex.test(String.fromCharCode(event.which))) return event.stopPropagation(),!1"

Hope this helps,

Marcel

https://tarunkumar.outsystemscloud.com/LiveMovie/WebScreen1.aspx?(Not.Licensed.For.Production)=

It takes the first time - or + sign after that it doesnt take - check the 3rd textbox

Hi,

I see what you mean. Did you try my code (if I look at the source of your page it shows the original code)?

ok

can you share me the oml please i am not getting it 


Solution

Tarun Kumar wrote:

ok

can you share me the oml please i am not getting it 


Just replace your code in the extended property with "var ex=/[\-\+]/; if (ex.test(String.fromCharCode(event.which))) return event.stopPropagation(),!1"

Solution

Marcel Lambrechts wrote:

Tarun Kumar wrote:

ok

can you share me the oml please i am not getting it 


Just replace your code in the extended property with "var ex=/[\-\+]/; if (ex.test(String.fromCharCode(event.which))) return event.stopPropagation(),!1"


thanx man it worked 

now one question how to make default to 1 


it by default showing as 0

Hi,

you can set the default value for the variable that is assigned to the textbox to 1?

Marcel Lambrechts wrote:

Hi,

you can set the default value for the variable that is assigned to the textbox to 1?

i got it thanks ...................


how can i prevent the special characters in text boxes using jquery