Lesson Code: http://www.developphp.com/video/JavaScript/Restrict-Text-Input-Characters-HTML-JavaScript-Tutorial
Learn to restrict one or more text fields in a form to allow only characters that you desire to be typed into them. We will remove undesirable characters in real time as the user types.
Original source
23 responses to “Restrict Text Input Characters HTML JavaScript Tutorial”
HTML5?
Hi Adam, great tutorial thank you… Can I do this for php or only works with html code?
Hey Adam, how can I add the same restrictions for numbersOnly? I want the input field to only allow users to type in figures btwn 50 000 and 200 000. Thanks
very nice tutorial. can I request tutorial validating input type file? extension and file size. to determine whether it is a virus or not. ex. virus.jpg. thx before ☺
am i understanding regex right if putting a-z only refers to lowercase letters?
ive been watching your tutorials and its been helping me so much…
thank u so much for that..
u can also restrict default key action and do something like this
function restrict(event,field){
var lastChar = String.fromCharCode(event.which);
var regex = new RegExp;
if(field == 'name'){
regex = /[A-z]|[b]|s|t|[(v + <tab>)]/gi;
}else if(field == 'message'){
regex = /[A-z0-9 ]|[^{}]/gi;
}if(!lastChar.match(regex)){
event.preventDefault();
}
}
Please i request all adam khuory viewer if you this video not like then not gave unlike.
this is more simple than I thought
Adam khoury is it possible to make an online multiplayer game on a website with action script 3?
do you use stackoverflow?
Usefull, thanx a lot
To make it more compact you save three letters by passing the regex var as a parameter.
function lettersOnly(input, r){
r=/[^a-z]/gi;
…
}
can you do a tutorial with firebase and angularjs? LOLs.
greal way of teaching the people … i have learn a great deal
can you do a money clock that show in real time exchange ..like these domation systems
If you don't want the "real time stripping as they type" you can use the "pattern" attribute of the input element in HTML to apply regular expression logic that will take effect when the user finishes the form and presses submit. But I think stripping the characters as they type is a cooler approach. Or use both! https://www.developphp.com/lib/HTML/input
thanks adam it was very helpfull , i want to ask if it could be possible to make a lettersOnly() function to accept only the arabic letters by using unicode or omething like this ??
May I ask what text editor you're using?
I know a few people with legal first name of "Maria de Jesus"
Don't forget, names can have – in them, So you should of had two functions, one for first name and one for last name, and allowed – in the last name! 🙂
Now show me how to restrict my wife's spending…..lol…. good tut man
You have a great voice. You sound like a young, friendly George Carlin!