ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
  • Generate PDF from HTML with CSS by using JavaScript and JQuery
  • HOW TO EXTRACT TEXT FROM IMAGE USING Angular(OCR with Tesseract.js / OCR using Angular)?
  • Angular 7: Create new project using Angular 7
  • How to use the bootstrap(ngx-bootstrap) 4 in angular 7?
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Disallow insertion of special characters in a form using JS

JavaScript Jquery about 7 years ago || 5/7/2019 || 2.9 K View

  • Hide

Disallow insertion of special characters in a form using JS:

In case of form validation most of us check the values inserted inside the form fields only after the user submits the form. So, would not it be more efficient if we could avoid or disallow user to insert those values that could lead to unsuccessful form submission. A better approach could be by replacing those   range of values using Regex and replacing it with an empty space.

It would be better to present it with an example.

Let us take a search bar where we have implemented the above method.

<div class = "form-group" >
    <div class = "form-line" >
    <label class = "font-weight-bold" > Search < /label>
<input type="text" id="Search" placeholder="Search"/>
 </div> 
  </div>

So we have a search bar where we don’t want the user to insert special characters. So here we can use the js as given below for checking the inserted values.

//For replacing special characters......
                $(document).on('keyup', "#Search", function () {
                    var h = $(this).val();
                    h = h.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
                    $(this).val(h);
                });

The above js inspects every key pressed within the search field and when any special character is inserted which is within the range of regex is automatically replaced with an empty space thus disallowing the user to insert any special characters.

Thus, This technique can be implemented for any type of form fields.

Also Read: Git commands you must know

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey