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.

Example:

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

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

        var errorFor = this.id;
        if (!isValidChar(inputText)) {
            $('#' + this.id).val('').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) {
	$("#error_sp_msg").remove();
	var k = e.keyCode,
			$return = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32  || (k >= 48 && k <= 57));
      if(!$return) {
      	$("<span/>",{
        	"id" : "error_sp_msg",
          "html" : "Special characters not allowed"
        }).insertAfter($(this));
      	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.

HTML:

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

jQuery:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.js"></script>
<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
    }
  });

});
</script>

Output:

how_to_restrict_special_characters_using_jquery_regex_output

 


You might also like:

Recommended Post
Featured Post
How To Add Watermark On Image In Laravel 10
How To Add Watermark On Image...

In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a...

Read More

Mar-31-2023

How To Avoid TokenMismatchException On Logout
How To Avoid TokenMismatchExce...

Many times we faced a Tokenmismatch error in laravel. This error occurs If you stay too long time on one form...

Read More

Jun-29-2020

How To Create Dark and Light Mode Website using jQuery
How To Create Dark and Light M...

In this article, we will see how to create a dark and light mode website using jquery. As you can see many websites and...

Read More

Nov-21-2020

Jquery Search Filter Example
Jquery Search Filter Example

In this small post i will so you jquery search filter example. here we will see how to search data using jquery filter....

Read More

Aug-25-2021