Er Dheeraj Pandey Er Dheeraj Pandey - 14 days ago 4
jQuery Question

Input text value must be start with W,T or Z other character will not Work on keyboard as a first character

Requirement is basically enter a text which Start with 'W','T' or 'Z' and then after we can insert any character or number. I have to use this on Keypress event if we press any key (Except W,T or Z) while entering text in field. The pressed key should not be visible on textbox.

For Example I am using following Script for mobile number validation :

$("[id$=InputTxt]").live('keypress', function(e) {
return (e.which !=8 && e.which !=0 && (e.which < 48 || e.which > 57)) ? false : true;


This is obviously not the shortest solution, but it's easy to read and adopt, as you can easily update the allowedCharacters in natural language, without fiddling around with keyCodes.

$("#customFormat").on("keypress", function(e) {
  var currentValue = $(this).val();
  var allowedCharacters = ['W', 'T', 'Z'];
  var newCharacter = String.fromCharCode(e.keyCode)

  if (currentValue != "" || allowedCharacters.indexOf(newCharacter) > -1) {
    return true;
  } else {
    return false;