Input validation to stop backSlash on keypress and copy paste | Code2night.com
Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
15 Aug
2022

Input validation to stop backSlash on keypress and copy paste

1376

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;
    }

  • |
  • AspNet MVC , Jquery , Validations

Comments

Follow Us On Social Media - Like Us On Facebook

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for New Subscribers!

Subscribe and Click on Verify and Download for download link

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

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2023 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier