jQuery: Restrict occurrence of specific word in textbox
Below jQuery code allows to restrict user to enter any specific word only once in textbox. For demo, I have used "article" word and it should occur only once in textbox. The code also makes case insensitive search.
$(document).ready(function () { $('#txtDesc').bind('keyup', function () { var txtToMatch = /article/gi; var iLimit = 1; var sMatch = $(this).val().match(txtToMatch); if (sMatch !== null && sMatch.length > iLimit) { $(".error").html("The word 'article' can occur only once."); } else { $(".error").html(""); } }); });
Related Post:
- jQuery code to allow only numbers in textbox
- jQuery code to allow numbers, alphabets or specific characters
If you want to make case sensitive search then, change "/article/gi" to "/article/g".
Feel free to contact me for any help related to jQuery, I will gladly help you.