Click here to Skip to main content
14,927,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
Wanted some help with inserting values in a text box with javascript validation wherein it should insert letters, numbers and certain special characters like
(\ . -) only.
What I used to do is var re = /^[a-zA-Z0-9]+$/; for the text box
which inserts letters and numbers.
and thought var re = /^[a-zA-Z\0-9-].+$/;would work but instead
it takes all special characters.Thank you.
Posted

1 solution

HTML
<input type="text" id='txtObj'  önkeypress="ISNumber(event)" />


Add following script:

JavaScript
function ISNumber(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
   
v3

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