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

How to avoid special characters by using OnClientKeyPressing event

1 Answer 148 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, 12:18 PM
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" 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Dec 2012, 04:13 AM
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.
Tags
ComboBox
Asked by
Joby
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or