validation code / function to allow only Letters in a text box



validation code / function to allow only Letters in a text box..

Answer / kevin konkle

//A script to format the Name field to only allow letters
//Have this be called off the keypress event of your
textbox control.
function nameFormat(fld, e)
{
var key = '';

//This string contains all characters you want to allow
//It could just as easily be 0123456789 to only allow
numbers.
var strCheck
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var whichCode = (window.Event) ? e.which : e.keyCode;

if (whichCode == 13) return true; // Enter
if (whichCode == 8) return true; // Delete

// Get key value from key code
key = String.fromCharCode(whichCode);

// Not a valid key
if (strCheck.indexOf(key) == -1) return false;

//If it is a legal value then add it to the text box
fld.value += key;

return false;
}

Is This Answer Correct ?    21 Yes 17 No

Post New Answer

More JavaScript Code Interview Questions

code to images to rollover

0 Answers  


code to set the main window's size

0 Answers  


How to test whether two strings are equal or not ?

1 Answers  


write a function to validate a given date

1 Answers  


Reading which Character Key was pressed

1 Answers  






How to Add Message and Confirmation Boxes Using JavaScript?

1 Answers  


write a code to generate pseudorandom numbes

0 Answers   ABC,


determine which key was pressed and its type

1 Answers  


code to keep a page Out of the browser history

0 Answers  


code to sorting an array of objects

0 Answers  


sample code to auto focusing the first field in a form

0 Answers   Wipro,


code to positioning of window in certain dimensions

0 Answers  


Categories
  • PHP Code Interview Questions PHP Code (33)
  • JSP Code Interview Questions JSP Code (6)
  • ASP Code Interview Questions ASP Code (5)
  • CGI Perl Code Interview Questions CGI Perl Code (3)
  • JavaScript Code Interview Questions JavaScript Code (63)
  • VB Script Code Interview Questions VB Script Code (20)
  • Shell Script Code Interview Questions Shell Script Code (31)
  • Python Code Interview Questions Python Code (34)
  • WinRunner Code Interview Questions WinRunner Code (1)
  • HTML DHTML XHTML Code Interview Questions HTML DHTML XHTML Code (13)
  • XML Interview Questions XML (43)
  • Scripts_Markup Code AllOther Interview Questions Scripts_Markup Code AllOther (5)