Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Input validation to stop backSlash on keypress and copy paste

Date- Aug 15,2022

5987

AspNet MVC Jquery

So for adding the validation we will copy this input box html on the page

    <div class="form-group row">
        <label for="inputPassword" class="col-sm-2 col-form-label">Paste remove some special character like "\"</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" placeholder="Enter Value" name="removePaste" id="removePaste" value="" />
        </div>
    </div>

Now we have to register keypress event to stop backslash on user type. So adding the event will stop users from enter backslash.

     $('#removePaste').on('keypress', function(event) {
        let keyPressed = event.keyCode || event.which;
        if (keyPressed == 92) {
            event.preventDefault();
            return false;
        }
    });

So, even after we stop back slash on kepress user can still copy and paste the backslash in the textbox, so we have to make user to stop copy paste slash using copy/paster. For that purpose we will add another paste event  and then replace the back slashes from blank .

     $('#removePaste').on('paste', function(event) {
        event.preventDefault();
        blockPaste(event, this);
    });

You have to add this function in your script. This is replacing the back slashes with blanks and thus helps us to stop user from invalid character. 

So this is how to apply Input validation to stop backslash on keypress and copy paste.

    function blockPaste(event, element) {
        var pastedData = event.originalEvent.clipboardData.getData('text/plain');
        var newString = pastedData.replace(/\\/g, '');
        console.log(newString);
        element.value = element.value + newString;
    }

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Convert Text to Speech in Asp.Net
Nov 06, 2023
How to export table data in pdf using itextsharp in asp.net mvc
Sep 16, 2023
How to generate pdf using itextsharp in asp.net mvc
Aug 06, 2023
Status Code 413 Request Entity Too Large
Jul 02, 2023

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers