I need a regex for c# and js which would allow only these characters within a string, other than those characters should be ignored.
Capital alphabets
Small alphabets
Numbers
Dot
Dash
Ampersand
Forward slash
Refer the below code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#txtTest').on("keyup", function () { var text = $(this).val(); text = text.replace(/[^a-zA-Z0-9.\-\&/ ]/g, ''); $(this).val(text); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <input name="txtTest" type="text" id="txtTest" /> </div> </form> </body> </html>
Demo
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.