Jump to content

regular expressions


gaya

Recommended Posts

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

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 by gaya
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...