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

How to avoid special characters in item request event

3 Answers 260 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Joby
Top achievements
Rank 1
Joby asked on 22 Mar 2012, 10:13 AM
Hi,
I have a radcombobox with OnClientKeyPressing event.User should not enter any special characters.How can i block this.I have used "OnClientKeyPressing " for validating for special characters.This is the JavaScript code
function OnClientKeyPressing() {
            var evtobj = window.event ? event : e
            if ((evtobj.shiftKey) && ((evtobj.keyCode == 188) || (evtobj.keyCode == 190))) {
                alert("Special characters not allowed");
                evtobj.keyCode = 0;
                evtobj.returnValue = false;
                return false;
            }
        }
Eg:-If the user type "vis<" then "<" it should not come.The value should be "vis" instead of "vis<".How can i avoid this.And data should come starting with "vis"

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Mar 2012, 12:28 PM
Hello,

Add the following javascript code which will handle any keydown event on the input area and will cancel it by returning false or preventing the default action.
JS:
<script type="text/javascript">
 function pageLoad()
  {
        var combo = $find("<%= RadCombobox1.ClientID %>");
        var input = combo.get_inputDomElement();
        input.onkeydown = onKeyDownHandler;
  }
 function onKeyDownHandler(e)
  {
   if (!e)
    e = window.event;
   var code = e.keyCode || e.which;
   if (e.shiftKey && code == 188 || code == 190)
    {
     e.returnValue = false;
     if (e.preventDefault)
      {
       e.preventDefault();
      }
    }
  
</script>
Also take a look into the following documentation for more information.
Prevent user from typing particular characters

Thanks,
Princy.
0
Joby
Top achievements
Rank 1
answered on 22 Mar 2012, 01:03 PM
Hi Princy,
Thank for reply.I have master page and many other page.I have already used page_load event in master page.I have so many drop down in my page so i cann't put every thing on page load.So i did using "OnClientKeyPressing" event.With the following code
function OnClientKeyPressing() {
            var e = window.event ? event : e;
            if (e != undefined) {
                var code = e.keyCode || e.which;
                if (e.shiftKey && code == 188 || code == 190) {
                    e.returnValue = false;
                    alert("Special characters not allowed");
                    if (e.preventDefault) {
                        e.preventDefault();
                    }
                }
            }
        }
Now with internet explorer its working and removing the "<" or ">" character.But with chrome its showing alert.But not removing the character.What else i need to do for removing?
0
Princy
Top achievements
Rank 2
answered on 22 Mar 2012, 01:32 PM
Hello,

One of the limitations of the OnClientKeyPressing event is that it cannot be cancelled. This means that the user can type any letter in the input area of the RadComboBox (when AllowCustomText is set to True). So one alternate method is to Add the above javascript code which will handle any keydown event on the input area and will cancel it by returning false (Internet Explorer) or preventing the default action (Firefox).

Thanks,
Princy.
Tags
ComboBox
Asked by
Joby
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Joby
Top achievements
Rank 1
Share this question
or