How To Restrict Special Characters Using jQuery Regex

Websolutionstuff | Dec-26-2022 | Categories : Laravel PHP jQuery

In this article, we will see how to restrict special characters using jquery regex. Here, we will learn how to create regex validation for a special character that is not allowed in the input textbox.

Using regular expression you can restrict the special character in the input fields. If you want to not be allowed special characters in input fields then you can use regex for validation.

So, let's see special characters not allowed using jquery regex and jquery validation for special characters not allowed.


In this example, we will check regex validation on keyup event.

$(function () {
   $("form :input[type='text']").on("keyup", function (e) {
        var inputTextId = $('#' +;
        var inputText = inputTextId.val();

        var errorFor =;
        if (!isValidChar(inputText)) {
            $('#' +'').valid();       
            setTimeout(function () { replaceErrorMsg(errorFor); }, 100);

function isValidChar(str) {
    return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);

function replaceErrorMsg(errorFor) {
   $("label[for='" + errorFor + "'].help-block").html("Input is Invalid.");


Example 2:

In this example, we will check validation using a special characters code.

<input type="text" id="name" >

$("#name").keypress(function(e) {
	var k = e.keyCode,
			$return = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32  || (k >= 48 && k <= 57));
      if(!$return) {
        	"id" : "error_sp_msg",
          "html" : "Special characters not allowed"
      	return false;


Example 3:

In this example, we will use a validation jquery CDN file. So, add the following code to your file. Check the documentation of the jquery validation plugin.


<form id="myform">
    <input type="text" name="title" />
    <input type="submit" />


<script src="[email protected]/dist/jquery.validate.js"></script>
$(document).ready(function() {

  $('#myform').validate({ // initialize the plugin
    rules: {
      title: {
        required: true,
        alphanumeric: true
    submitHandler: function(form) { // for demo
      alert('valid form submitted'); // for demo
      return false; // for demo





You might also like:

Recommended Post
Featured Post
How To Get Children Element In jQuery
How To Get Children Element In...

In this article, we will see how to get the children of this selector in jquery. You can use the find() method...

Read More


How to Filter Datatable using Dropdown in Laravel 10
How to Filter Datatable using...

Hello developers! 👋 Ever found yourself dealing with a DataTable in Laravel and wished for a nifty way to filter th...

Read More


How To Create Custom Login Page In Django
How To Create Custom Login Pag...

In this article, we will see how to create a custom login page in django. how to allow user registration, login, an...

Read More


Drag And Drop Div Using jQuery
Drag And Drop Div Using jQuery

in this article, we will see drag and drop div using a jquery example. For this example, we are using sortable js. ...

Read More