How to Allow Only Special Characters and Spaces in Text Field Using jQuery ?
Last Updated :
28 Apr, 2025
We are going to create an input box that only contains the special characters and the space as the input. It will show an error if any other character is entered. We will use regular expressions that consist of only special symbols and spaces with different events in jQuery.
In this approach, we will use the regular expression with the input event that checks the correctness of the entered input at the same time and write the respective text for the same.
Syntax:
$('input_selector').on('input', function(){});
Example: The below code example will help you understand the use of the regex with input event to check for the correct input.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>
How to allow only special characters and
spaces in text field using jQuery?
</title>
<style>
.container{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1 style="color: green">
GeeksforGeeks
</h1>
<h2>
The below input will take only the <br/>
special characters and spaces as input.
</h2>
<input type="text" id="inp">
<p class="result"></p>
</div>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
integrity=
"sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script>
$(document).ready(() => {
$('#inp').on('input', function (e) {
const enteredVal = $(this).val();
let result = $('.result');
let regex = /^[\s!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]+$/g;
if (regex.test(enteredVal)) {
result.html(
`<b style="color: green">
The entered value is correct!!
</b>`)
}
else {
result.html(
`<b style="color: red">
The entered value is incorrect!!
</b>`)
}
});
});
</script>
</body>
</html>
Output:

By comparing the ASCII codes of the pressed keys
In this method, we will check the ASCII value of the currently pressed key and compare it with the ASCII values of the special symbol and the space. It will compare the entered key once the key is pressed.
Syntax:
$('input_selector').on('keypress', function(e){});
Example: The below code example will help you understand and implement this approach practically.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>
How to allow only special characters
and spaces in text field using jQuery?
</title>
<style>
.container{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1 style="color: green">
GeeksforGeeks
</h1>
<h2>
The below input will take only the <br/>
special characters and spaces as input.
</h2>
<input type="text" id="inp">
<p class="result"></p>
</div>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"
integrity=
"sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g=="
crossorigin="anonymous"
referrerpolicy="no-referrer">
</script>
<script>
$(document).ready(() => {
$(document).on('keypress', '#inp', function (e) {
let result = $('.result');
const pressedKey = e.key;
let pressedKeyCode = e.charCode;
if ((pressedKeyCode >= 32 && pressedKeyCode <= 47) ||
(pressedKeyCode >= 58 && pressedKeyCode <= 64) ||
(pressedKeyCode >= 91 && pressedKeyCode <= 96) ||
(pressedKeyCode >= 123 && pressedKeyCode <= 126))
{
e.preventDefault();
result.html(`
<b style="color: green">
The pressed key
<em>
'${pressedKey}'
</em>
is a Valid input!!
</b>`)
}
else {
e.preventDefault();
result.html(`
<b style="color: red">
The pressed key
<em>
'${pressedKey}'
</em>
is not a Valid input!!
</b>`)
}
});
});
</script>
</body>
</html>
Output:

Similar Reads
How to set different color for each letter in a text field using jQuery ? In this article, we will find how to set a different color for each letter in a text field using jQuery? Approach: To do this we will have to change the color of text whenever the user enters a letter using the keyboard. For this, we will use onkeydown event that triggers whenever the user presses a
2 min read
How to create a Text Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be a Text Input using jQuery Mobile. Approach: First, we add the jQuery Mobile scripts needed for the project. <link rel=âstylesheetâ hre
1 min read
How to create a Disabled Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Disabled Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hre
1 min read
How to create a Textarea Input Box using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be a Textarea Input box using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ href=âht
1 min read
How to select all on focus in input using JQuery? The select() event is applied to an element using jQuery when the user makes a text selection inside an element or on focus the element. This event is limited to some fields. Syntax: $("selected element").select(); //Select all Input field on focus $("input").select(); //Select Input field on focus
1 min read
How to prevent a text field losing focus using jQuery ? In this article, we will learn how to prevent a textfield or an input from losing focus using jQuery. This can be used in situations where user validation is required. There are two approaches that can be taken: Approach 1: We will be using the jQuery bind(), val(), preventDefault() and focus() meth
3 min read