Check whether user enter specific character on TextBox KeyPress event using jQuery

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Dear sir,

i have a textbox when user type in textbox 123 show alert message Kindly use Q firstly else if user type Q123 alert message kindly continue your activities.

i used below code but its not get proper value 

<script type="text/javascript">
    function GetcharCode(evt) {
        var charCode;
        if(typeof(evt.charCode) != "Q")
        {
            keyCode = evt.charCode;
        }
        alert("kindly continue your activities");
        else if
        {
            alert("kindly continue your activities");
        }
    }
</script>

 

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Hi rajeesh,

Check the below code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <h3>Using KeyUp</h3>
        <script type="text/javascript">
            function CheckValueOnKeyUp(ele) {
                if (ele.value.indexOf("Q") != 0 && ele.value != '') {
                    alert("Kindly use Q as first letter.");
                }
            }
        </script>
        <input type="text" onkeyup="CheckValueOnKeyUp(this)" />
        <br />
        <hr />
        <h3>Using Blur</h3>
        <script type="text/javascript">
            function CheckValueOnBlur(ele) {
                if (ele.value.indexOf("Q") != 0 && ele.value != '') {
                    alert("Kindly use Q as first letter.");
                }
            }
        </script>
        <input type="text" onblur="CheckValueOnBlur(this)" />
    </div>
</body>
</html>

Demo