javascript - JQuery to allow only alphabets, numeric and forward slash in TextBox -


i have text field in asp.net , want allow alphanumeric , forward slash (/) keys in that. tried following code,

function jscheckinput(e) {     var evt = (e) ? e : window.event;     var key = (evt.keycode) ? evt.keycode : evt.which;     if (key != null) {         key = parseint(key, 10);         if (key < 47 || (key > 57 && key < 65) || (key > 90 && key < 97) || key > 122) {             if (!jsisuserfriendlychar(key)) {                 return false;             }         }         else {             if (evt.shiftkey) {                 return true;             }         }     }     return true; }  function jsisuserfriendlychar(val) {     // backspace, tab, enter, insert, , delete       if (val == 8 || val == 9 || val == 13 || val == 45 || val == 46) {         return true;     }     // ctrl, alt, capslock, home, end, , arrows       if ((val > 16 && val < 21) || (val > 34 && val < 41)) {         return true;     }     return false; } 

in web forms page added below,

<asp:textbox id="text_value" cssclass="textbox" onkeydown="return jscheckinput(event);" runat="server"></asp:textbox> 

here able enter alphabets , numbers not able enter value /. have enabled shift key can give shift + ? enter forward slash. problem when press shift + numeric key special characters there ! @ # $ % ^ & * ( ) ... coming in tet field. doing wrong here?

if want use regular expression. ignore if don't

const regex = /^[a-z0-9\/]+$/gi; const str = `asdasdas/asdfaasdasda`; //test string  if(regex.test(str )){      console.log('allowed'+str);   } 

tested here


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -