I want to allow only alphabets and numbers and no special characters in my textbox.
I want to do this using Regular Expressions and JavaScript. Please post an example
Try this
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> function Validate(txt) { txt.value = txt.value.replace(/[^a-zA-Z 0-9\n\r]+/g, ''); } </script> </head> <body> <form id="form1"> <input type = "text" id = "txt" onkeyup = "Validate(this)" /> </form> </body> </html>
Demo
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> function Validate(txt) { txt.value = txt.value.replace(/[^a-zA-Z 0-9\n\r.]+/g, ''); } </script> </head> <body> <form id="form1"> <input type = "text" id = "txt" onkeyup = "Validate(this)" /> </form> </body> </html>
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.