How to avoid special characters by using OnClientKeyPressing event

2 posts, 0 answers
  1. Joby
    Joby avatar
    25 posts
    Member since:
    Nov 2010

    Posted 22 Mar 2012 Link to this post

    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 remove.The value should be "vis" instead of "vis<".Now i am getting value as "vis<" and alert is showing.How can i avoid this and data should come starting with "vis" 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Dec 2012 Link to this post

    Hi Joby,

    Try the following code snippet to achieve your scenario.

    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) && ((e.keyCode == 188) || (e.keyCode == 190))) {
                alert("Special characters not allowed");
                evtobj.keyCode = 0;
                e.returnValue = false;
                if (e.preventDefault) {
                    e.preventDefault();
                }
            }
        }
    </script>

    Hope this helps.

    Regards,
    Princy.
Back to Top