We have one text Field.We know how to restrict special characters.But We need Allow alphabet and Numbers and hyphen(-) only.No need Sepcial characters but except (-) . Give me any idea.
Mycode:
$('#pduration').keydown(function (e) { if (e.shiftKey || e.ctrlKey || e.altKey) { e.preventDefault(); } else { var key = e.keyCode; if (keyCodeEntered == 45) { // Allow only 1 minus sign ('-')... if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0)) return false; else return true; } } });
If we tried this code it’s restrict spectal charecters but it’s allow -,/,+ Please guide me only allow number and alphabet and hyphen only
Advertisement
Answer
replace this section:
if (keyCodeEntered == 45) { // Allow only 1 minus sign ('-')... if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0)) return false; { else return true; }
with this:
// keys a-z,0-9 numpad keys 0-9 minus sign backspace if ( ( key >= 48 && key <= 90 ) || ( key >= 96 && key <= 105 ) || key == 109 || key==8) { //return true; } else { //return false } })