How do I block special characters from input box in HTML

Home / Answer

How do I block special characters from input box in HTML

Author | Asked on 2024-10-31 | • HTML, CSS

With the simple use of a regular expression, you can block special characters from the input box in HTML.

 <script type="text/javascript">
    $('input#username_go').on('keypress', function (event) {
        var regex = new RegExp("^[a-zA-Z0-9]+$");
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        if (!regex.test(key)) {
           event.preventDefault();
           return false;
        }
    });
</script>    

 

Please login your account for replying

Do you want to get the latest update?