views:

318

answers:

2

I want to allow numbers only to be allowed for a textbox and I already do it onkeydown event by allow only numbers and prevent ctrl+V but I have two problems :

  1. if I make right click then paste so any char can be entered and I want a solution without disable right click by oncontextmenu="return false;"

  2. if I drag and drop any text it will be entered

Is there any solution that can work in all browsers without problem ?

+1  A: 

Just filter out the non numeric chars in the onchange event of your textbox and set it back to the textbox.

Ovidiu Pacurar
this will allow to enter any char as change event fired after textbox lose focus ... what i need is to not allow user to enter other than number
Space Cracker
A: 

You can use onpaste event. You will be able to catch whenever something is pasted.
You can refer to Andy's answer regarding onpaste event.

Ahmed
onpaste doesn't work in all browsers ... that's the problem
Space Cracker
In that case you have to handle onchange event as Ovidiu said.
Ahmed