show-notice
hide-notice

Thursday 15 August 2013

jQuery: Restrict occurrence of specific word in textbox


Introduction:

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.

Example:

$(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("");
        }
    });
});



SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com