Rasul
Rasul

Reputation: 71

prevent characters in text box in php using jquery

I want to prevent user to input few characters like ; and \ and /. I now it need jquery to do, but i cant find one

The jquery will used in name. zip code input to prevent such characters etc.

please show me an example as i new to jquery and php

Upvotes: 2

Views: 83

Answers (2)

Mohammad
Mohammad

Reputation: 21489

You can use event.which to getting code of pressed key and use return false to preventing enter of character.

$("input").keydown(function(e) {
    if (e.which === 186 || e.which === 191 || e.which === 220){
        console.log("This character can't entered");
        return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />

Upvotes: 1

ksno
ksno

Reputation: 487

This should do the job. I hope you can read this simple code:

// select css classes 'input' and assign onKeyDown event to them
$(".input").keydown(function(event) {
    // check what key is pressed. dot is not allowed in  this case
    if ( event.keyCode === 190) {
        // prevent event to happen
        event.preventDefault();
    }
});

On the bottom of this page there is a small table for some codes and also an input field which shows you key code for particular key. Just as a helper, to let you find codes for wished key faster.

Upvotes: 1

Related Questions