Restrict Text Input Characters HTML JavaScript Tutorial




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”

  1. 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();
    }
    }

Leave a Reply