How to allow only english,numeric and special chars in textbox using jquery or javascript?
Solution 1:
The JavaScript replace method can be implemented with a regex pattern to filter out specific inputs in a textbox. Give this code a try and customize it to suit your needs.
You can test this by accessing the provided jsFiddle link: http://jsfiddle.net/leniel/rtE54/
Following additional contemplation, I executed this block of code.
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase, digits 0 to 9 and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetDigitsAndWhiteSpace = /[A-Za-z0-9 ]/g;
// Retrieving the key from the char code passed in event.which
// For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
var key = String.fromCharCode(event.which);
//alert(event.keyCode);
// For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
// keyCode == 8 is backspace
// keyCode == 37 is left arrow
// keyCode == 39 is right arrow
// englishAlphabetDigitsAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetDigitsAndWhiteSpace.test(key)) {
return true;
}
// If we got this far, just return false because a disallowed key was typed.
return false;
});
$('#mytextbox').on("paste",function(e)
{
e.preventDefault();
});
For further information, refer to this article on how to utilize JavaScript regex + jQuery for restricting input text boxes to English characters/letters only.
Solution 2:
Restrict the keys by verifying if they fall within the boundaries you establish, based on onkeypress
.
function ValidateKey()
{
var key=window.event.keyCode;
var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';
return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
}
and you can use it as
The code can also be found on JSFiddle at this link: http://jsfiddle.net/UHGRz/3/
Applying jQuery to input controls works universally, except for copy/paste scenarios where the Lenier solution with Replace is necessary.
And a convert to jQuery code
jQuery("input").keypress(function()
{
var key=window.event.keyCode;
var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';
return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
})
The code can be accessed on both JSFiddle and Fiddle, with the link http://jsfiddle.net/UHGRz/5/.
Solution 3:
if ch is that char,you can do this
if((ch.charCodeAt(0)>="a".charCodeAt(0) && ch.charCodeAt(0)<="z".charCodeAt(0))||(ch.charCodeAt(0)>="A".charCodeAt(0) && ch.charCodeAt(0)<="Z".charCodeAt(0)))
You can perform a similar contrast using Arabic characters.
which is represented in unicode
[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]
Allow only positive and negative numbers in textbox, I have a textbox called count. I want to enter only positive and negative numbers in the textbox using JavaScript or jQuery regular expression. Do not allow «text» and «decimals» and «special characters» in the textbox. Even copy and paste also it should take only positive or negative numbers. Important:
How to allow all special characters in textbox only one time using JS?
Solution:
Develop a function that captures keypresses on the designated textarea and verifies if each entered character is permissible by comparing it against event.which
. If the character is not permitted, either return false or execute event.preventDefault() to prevent it from being entered.
Javascript — Allow only alphanumeric and, I would like to only allow alphabet, special character only a dash (-) and integers in a textbox. And the number i would like to limit them to from 1 to 100. Example A-100. This code allows only numbers to be inserted in the textbox. I want it to allow so that it is something like A-100. Any idea or can someone help …
How to allow only numbers and special character injavascript
Solution:
User-691245060 posted
Here’s a link to a sample site that demonstrates the usage of AJAX Modalpopup: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ModalPopup/ModalPopup.aspx
thanks,
Combine the RegularExpressionValidator with a regular expression pattern similar to this — ^[0-9#$%^&*()@!]*$.
thanks,
How to allow only english,numeric and special chars in, I want to allow only English,numeric and special characters to be typed in my web page. i want to apply this thing using jquery or javascript. Actually my application is in 2 languages so for that i want to do this. I want to do the same thing with Arabic language too.. please help me. How can I do that?
Read other technology post: What exactly is steady state solution