gaya Posted February 26, 2013 Share Posted February 26, 2013 The following is my code. function noAlphabets(evt,val) {var no=val; var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode != 46 && charCode!=8 && charCode > 31 && (charCode < 48 || charCode > 57 ) ) { return false; }else{if ((/^[1-4]/.test (val))) {//alert ("No should be within 1 character");return false;}}}<td>correct Answer<font color="red">*</font></td><td>:</td> <td><input type="text" name="corr_ans" id="corr_ans" size="16" onkeypress="return noAlphabets(event, this.value)"></td> the backspace key is not working.Please anyone help me. I dont know what i'm doing wrong here. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 26, 2013 Share Posted February 26, 2013 Backspace should normally be 8, but you should log the key codes on the console to check what the code is on your machine. Link to comment Share on other sites More sharing options...
gaya Posted February 27, 2013 Author Share Posted February 27, 2013 (edited) My code is not working. Please help me by giving some sample code to restrict the digit in the textbox with backspace working.Otherwise please tell me what else the changes to be made in the above code. Edited February 27, 2013 by gaya Link to comment Share on other sites More sharing options...
justsomeguy Posted February 27, 2013 Share Posted February 27, 2013 Like I said, you should print the keycodes to the console so that you can verify that backspace is actually 8. Maybe it's not 8. Link to comment Share on other sites More sharing options...
gaya Posted February 28, 2013 Author Share Posted February 28, 2013 Thanks justsomeguy. I did it. But not by printing the keycodes. Actually in my above code i used the regular expression for entering only digit. So when i'm trying to delete the entered digit using backspace, it simply consider the backspace as seperate event. So the back space wont work in this code. if ((/^\d{1}/.test (val))) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now