Validar solo el ingreso de String con Javascript


/ Published in: JavaScript
Save to your folder(s)

Valida por medio del metodo onkeypress de un campo input, que solo se ingresen string /[a-zA-Z]/


Copy this code and paste it in your HTML
  1. /**
  2. *
  3. * @permite ingresar sólo letras
  4. * @versión:1.0.0 @modificado:xx de xx de xxxx
  5. * @autor: Jaime Quiñones
  6. *
  7. */
  8. function stringCheck(e, field) {
  9.  
  10. key = e.keyCode ? e.keyCode : e.which
  11.  
  12. //alert(key);
  13. //backspace
  14. if(key == 32) return true;
  15.  
  16. //numeros 0-9
  17. if((key >= 48 && key <= 57) || key == 42) return false;
  18.  
  19. // Caracteres Raros
  20. if( key == 94 ||
  21. key == 59 ||
  22. key == 58 ||
  23. key == 61 ||
  24. key == 43 ||
  25. key == 45 ||
  26. key == 95 ||
  27. key == 47 ||
  28. key == 96 ||
  29. key == 40 ||
  30. key == 41 ||
  31. key == 91 ||
  32. key == 123 ||
  33. key == 92 ||
  34. key == 124 ||
  35. key == 125 ||
  36. key == 93 ||
  37. key == 39 ||
  38. key == 34 ||
  39. key == 44 ||
  40. key == 46 ||
  41. key == 47 ||
  42. key == 231 ||
  43. key == 42 ||
  44. key == 168 ||
  45. key == 33 ||
  46. key == 161 ||
  47. key == 183 ||
  48. key == 8364 ||
  49. key == 37 ||
  50. key == 191 ||
  51. key == 186 ||
  52. key == 170 ||
  53. key == 64 ||
  54. key == 35 ||
  55. key == 126 ||
  56. key == 36 ||
  57. key == 38 ||
  58. key == 180 ||
  59. key == 63) return false;
  60.  
  61. }
  62.  
  63.  
  64. <!-- EN UN INPUT -->
  65. onkeypress="return stringCheck(event, this);"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.