Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
Posts: 28
Registered: ‎08-19-2008
My Device: Not Specified

capture input key on browser

I have a web page with a textbox, and just allow numeric input. On IE, I use javascript to capture the keydown event of that field. However, it doesn't work on Blackberry Browser. How can I do it? Any sample? Thanks.

 

Here is the javascript I am using:

 

<script language="javascript" type="text/javascript">

 function chkNum(e) {

var key;

if(window.event) // IE

{

key = e.keyCode;

}

else if(e.which)

{

key = e.which

}

if(key != null) {

key = parseInt(key, 10);

alert(key);

if((key < 48 || key > 57) && (key < 96 || key > 105)) {

// Backspace, Tab, Enter, Insert, and Delete

 if(key == 8 || key == 9 || key == 13 || key == 45 || key == 46)

return true;

// Ctrl, Alt, CapsLock, End, Home, and Arrows

 else if((key >= 17 && key <= 20) || (key >= 35 && key <= 40))

return true;

else

 return false;

}

else {

if(e.shiftKey)

return false;

}

}
  return true;

}

</script>

BlackBerry Development Advisor
Posts: 15,753
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: capture input key on browser

[ Edited ]

What BlackBerry model and handheld software version are you testing on?  You can find this under Options, About on the BlackBerry handheld.

Message Edited by MSohm on 12-10-2008 04:18 PM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
Posts: 28
Registered: ‎08-19-2008
My Device: Not Specified

Re: capture input key on browser

I've tested on Bold Simulator (4.6.0) but it doesn't work. Is there anything wrong in my Javascript? But it workes fine on IE.
BlackBerry Development Advisor
Posts: 15,753
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: capture input key on browser

Please verify that JavaScript is enabled in the BlackBerry Browser.  To do so open the browser, select Options from the menu, then Browser Configuration and check off Support JavaScript.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
Posts: 10
Registered: ‎08-22-2008
My Device: Not Specified

Re: capture input key on browser

Hi, in version 4.6, window object doesn't have event property (like IE) and Event object doesn't have wich property (like Firefox). So, you juste have to extract keyCode like this :

var key = e.keyCode;

 

But I don't know how prevent typing a non numeric character. If your function return false, it doesn't work.

If you prevent the default action with e.preventDefault(); it doesn't work.

 

I have the same problem and I dont have any solution.

Perhaps some Javascript guru ?

New Developer
Posts: 10
Registered: ‎08-22-2008
My Device: Not Specified

Re: capture input key on browser

The only solution I found is to filter input field value :

<input type="text" onkeypress="filterChar(e, this)" .../>

 

function filterChar (e, obj)

{

// test control character before : backspace, home, etc.

...

 

// input value property contains all (printable) characters input

// I use regular expression for deleting all non numeric character : character that is not 0 to 9 or + or - or 'space' for

// grouping separator or '.' for decimal separator

obj.value = obj.value.replace(/[^0-9\-\+\ \.]/g, '');        
}

New Developer
Posts: 28
Registered: ‎08-19-2008
My Device: Not Specified

Re: capture input key on browser

yes, we can use this way, but not very well.

I tried this on 4.3, it doesn't work. But it works on 4.6. How can I do this on old version device without upgrade? Any suggestion?

 

<script language="javascript" type="text/javascript">
    function chkNum(e,obj) {
        obj.value = obj.value.replace(/[^0-9\-\+\ \.\#]/g, '');       
    }

</script>

<input type="text" id="txtIn" onkeyup="chkNum(event,this)" />

Highlighted
Developer
Posts: 107
Registered: ‎09-25-2008
My Device: Not Specified

Re: capture input key on browser

There's a simpler way to accomplish what you're trying to do. The input field should support the -wap-input-format CSS style, so all you need to do is add that to your input field: <input type='text' style='-wap-input-format: "*N"'>