This is a migrated thread and some comments may be shown as answers.

[Solved] Prevent certain characters in Firefox

3 Answers 117 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Waylon Campbell
Top achievements
Rank 1
Waylon Campbell asked on 19 Nov 2009, 10:23 PM
I need to restrict input in the combobox so that it only allows the numbers 0-9 and the backspace key.  I have tried the code here and here.  The code works in IE, but not in Firefox 3.5.

<telerik:RadComboBox ID="RadComboBox1" runat="server"  
    EnableLoadOnDemand="true"  
    AllowCustomText="true"  
    AutoPostBack="true"  
    OnItemsRequested="RadComboBox1_ItemsRequested" 
    OnTextChanged="RadComboBox1_TextChanged"  
    HighlightTemplatedItems="true" > 
    <ItemTemplate> 
        <div><%#DataBinder.Eval(Container, "Text") %></div
        <div><%#DataBinder.Eval(Container, "Attributes['Name']")%></div
        <div><%#DataBinder.Eval(Container, "Attributes['Number']")%></div
    </ItemTemplate> 
</telerik:RadComboBox> 

        function pageLoad() {             
            var combo = $find("<%= RadComboBox1.ClientID %>"); 
            var input = combo.get_inputDomElement(); 
            input.onkeydown = ComboBoxKeyDownHandler; 
        } 
 
        function ComboBoxKeyDownHandler(e) 
        { 
           if (!e) 
               e = window.event; 
                
           var code = e.keyCode; 
 
           //only allow 0 through 9 or backspace to be entered 
           if (!((code >= 48 && code <= 57) || (code == 8))) 
           { 
               e.returnValue = false
               if (e.preventDefault) 
               { 
                   e.preventDefault(); 
               } 
           } 
        }  

3 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 20 Nov 2009, 11:22 AM
Hi Waylon Campbell,

In FireFox you could use event.which to obtain the pressed key's code:

var code = e.keyCode || e.which;

Kind regards,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Waylon Campbell
Top achievements
Rank 1
answered on 20 Nov 2009, 03:44 PM
I modified the code per your suggestion but it still doesn't work in FireFox.  It looks like the e.preventDefault(); line is not working.  I know the the line is executed but I can still type invalid characters in the combobox when using FireFox.
0
Accepted
Simon
Telerik team
answered on 23 Nov 2009, 02:12 PM
Hello Waylon Campbell,

Indeed. You need to return false from the function to prevent the default action in FireFox:

function ComboBoxKeyDownHandler(e) {
    if (!e) e = window.event;
    var code = e.keyCode || e.which;
 
    //only allow 0 through 9 or backspace to be entered
    if (!((code >= 48 && code <= 57) || (code == 8))) {
    e.returnValue = false;
 
    if (e.preventDefault)
        e.preventDefault();
         
    return false;
    }
}

Sincerely yours,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
Waylon Campbell
Top achievements
Rank 1
Answers by
Simon
Telerik team
Waylon Campbell
Top achievements
Rank 1
Share this question
or