Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team
i want to perform a validations OnkeyPress JQuery Event.
I want to do validation for Alphabets,delete,backspace ,left arrow,up arrow,right arrow,and down arrow using JavascriptkeyCode in one Textbox..How to do this? ..kindly Help..

I have tried this
function keyPressFunctionForValidations(e) {<br />
               debugger;<br />
               var code = e.keyCode || e.which;<br />
              if (code != 32  (code <65 || code > 90)) {<br />
                   return<br />
               }<br />
           }



Thanks
Harshal Raut
Posted

1 solution

try this..

JavaScript
<script type="text/javascript">
        $(function () {
        $('#txt').keypress(function (e) {

            var charCode = e.keyCode || e.which;
            if (charCode == 37 || charCode == 39) return true;  // allow arrows
            if (charCode == 46) return true; //delete
            if (charCode == 190 || charCode == 110) return true; // period or dot
            if (charCode == 35 || charCode == 36) return true; // home, end
            if (charCode == 8 || charCode == 9) return true; // backspace , tab
            if (charCode > 64 && charCode < 91) return true;  // a - z
            if (charCode > 96 && charCode < 123) return true; // A- Z
            e.preventDefault();
        });

        });
    </script>
 
Share this answer
 
Comments
[no name] 11-Jan-14 5:41am    
Thank you so much for giving the answer.
Is it possible to write this statement in one If condition...
If is it possible please let me know..

Thanks
Harshal
Karthik_Mahalingam 11-Jan-14 5:44am    
yes you can, need to add an [ or || ] conditions for each case. thats it..
[no name] 11-Jan-14 5:49am    
i want to write like this.Please check it

if (charCode == 37 || charCode == 39) return true; ||
(charCode == 46) return true; ||
(charCode == 190 || charCode == 110) return true; ||
(charCode == 35 || charCode == 36) return true;||
(charCode == 8 || charCode == 9) return true; ||
(charCode > 64 && charCode < 91) return true; ||
(charCode > 96 && charCode < 123) return true;
e.preventDefault();

Thank you ..
harshal
Karthik_Mahalingam 11-Jan-14 5:51am    
no no not like this..
Karthik_Mahalingam 11-Jan-14 5:52am    
like this
if ( (charCode == 37 || charCode == 39) || (charCode == 46) || (charCode == 190 || charCode == 110)................ )
return true;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900