kisukedeath

Validaciones campos - Jquery Validate

Sep 13th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.69 KB | None | 0 0
  1. //Validacion para campos en la que se ingresará empresas, acepta letras con tilde o sin tilde, ampersands y puntos
  2. jQuery.validator.addMethod("valiEmpresa", function(value, element) {
  3.     return this.optional(element) || /^[a-zA-Z0-9&.áéíóúÁÉÍÓÚäëïöüÄËÏÖÜàèìòùÀÈÌÒÙñÑ]*$/.test(value);
  4. });
  5.  
  6. //Validacion para campos que contengan paginas web
  7. jQuery.validator.addMethod("valiWeb", function(value, element) {
  8.     return this.optional(element) || /^[a-zA-Z0-9]+[a-zA-Z0-9-\.]*(\.[a-zA-Z]{2,3})$/.test(value);
  9. });
  10.  
  11. //validacion para campos que contengan direccion
  12. jQuery.validator.addMethod("valiDireccion", function(value, element) {
  13.     return this.optional(element) || /^[a-zA-Z0-9.&//áéíóúÁÉÍÓÚäëïöüÄËÏÖÜàèìòùÀÈÌÒÙñÑ]*$/.test(value);
  14. });
  15.  
  16. //validacion para campos que contenga email
  17. jQuery.validator.addMethod("valiEmail", function(value, element) {
  18.     return this.optional(element) || /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/.test(value);
  19. });
  20.  
  21. //validacion para combos simple
  22. $.validator.addMethod('selectnone',function(value, element) {
  23.   return this.optional(element) || (value!='seleccione');
  24. }, "Seleccione una opcion");    
  25.  
  26.  
  27. //Validación sólo letras en mayúsculas/minúsculas, acentudadas, espacios , punto, coma. No admite cadena vacía
  28. function validaran(e){
  29.         obj=e.srcElement || e.target;
  30.         tecla_codigo = (document.all) ? e.keyCode : e.which;
  31.         if(tecla_codigo==8)return true;
  32.         patron =/^[a-zA-ZáéíóúÁÉÍÓÚäëïöüÄËÏÖÜàèìòùÀÈÌÒÙñÑ\s.,0-9]+$/;
  33.         tecla_valor = String.fromCharCode(tecla_codigo);
  34.         return patron.test(tecla_valor);
  35.  
  36. }
  37.  
  38.  
  39. //Validación para monto mayor a 0
  40. $.validator.addMethod("morethan0", function(value, element) {
  41.   return this.optional(element) || (parseFloat(value) > 0);
  42. }, "* El monto debe ser mayor a 0");
  43.  
  44. //Validación solo números, no se admite cadena vacía
  45. function validarn(e){
  46.         obj=e.srcElement || e.target;
  47.         tecla_codigo = (document.all) ? e.keyCode : e.which;
  48.         if(tecla_codigo==8)return true;
  49.         patron =/^[0-9]+$/;
  50.         tecla_valor = String.fromCharCode(tecla_codigo);
  51.         return patron.test(tecla_valor);
  52.        
  53. }
  54.  
  55. //Validación fecha formato yyy/mm/dd ( si se quiere cambiar solo mover 4 y 1,2)
  56.  $.validator.addMethod("formatfecha", function(value, element) {
  57.   return value.match(/^(\d{4}\-\d{1,2}\-\d{1,2})$/);
  58. }, "Por favor ingrese un formato valido");
  59.  
  60.  
  61. //Validacion para DNI + maxlength="8"
  62. function validarsolon(e){
  63.         obj=e.srcElement || e.target;
  64.         tecla_codigo = (document.all) ? e.keyCode : e.which;
  65.         if(tecla_codigo==8)return true;
  66.         patron =/[0-9]/;
  67.         tecla_valor = String.fromCharCode(tecla_codigo);
  68.         control=(tecla_codigo==46 && (/[.]/).test(obj.value))?false:true
  69.         return patron.test(tecla_valor) &&  control;
  70.  
  71.         }
  72.  
  73. //Validación para solo letras
  74. function validarsolol(e){
  75.         obj=e.srcElement || e.target;
  76.         tecla_codigo = (document.all) ? e.keyCode : e.which;
  77.         if(tecla_codigo==8)return true;
  78.         patron =/^[a-zA-ZñÑáéíóúÁÉÍÓÚ]+$/;
  79.         tecla_valor = String.fromCharCode(tecla_codigo);
  80.         return patron.test(tecla_valor);
  81.  
  82. }
  83.  
  84. -----------------------------------------------------------------------------------------------------------------
  85. function validate(evt) {
  86.     var theEvent = evt || window.event;
  87.     var key = theEvent.keyCode || theEvent.which;
  88.     key = String.fromCharCode( key );
  89.     var regex = /[0-9]/;
  90.     if( !regex.test(key) ) {
  91.         theEvent.returnValue = false;
  92.         if(theEvent.preventDefault) theEvent.preventDefault();
  93.     }
  94. }
  95.  
  96. <input type="text" id="units" name="units" onkeypress="validate(event)" maxlength="3">
  97.  
  98.  
  99. function validarn(e){
  100.     obj=e.srcElement || e.target;
  101.     tecla_codigo = (document.all) ? e.keyCode : e.which;
  102.     if(tecla_codigo==8)return true;
  103.     patron =/[0-9.]/;
  104.     tecla_valor = String.fromCharCode(tecla_codigo);
  105.     control=(tecla_codigo==46 && (/[.]/).test(obj.value))?false:true
  106.     return patron.test(tecla_valor) &&  control;
  107. }
  108.  
  109. function validaran(e){
  110.     obj=e.srcElement || e.target;
  111.     tecla_codigo = (document.all) ? e.keyCode : e.which;
  112.     if(tecla_codigo==8)return true;
  113.     patron =/^[a-zA-ZñÑáéíóúÁÉÍÓÚ\s.,0-9]+$/;
  114.     tecla_valor = String.fromCharCode(tecla_codigo);
  115.     return patron.test(tecla_valor);
  116. }
  117.  
  118. function validarnum(e){
  119.     obj=e.srcElement || e.target;
  120.     tecla_codigo = (document.all) ? e.keyCode : e.which;
  121.     if(tecla_codigo==8)return true;
  122.     patron =/^[0-9]+$/;
  123.     tecla_valor = String.fromCharCode(tecla_codigo);
  124.     return patron.test(tecla_valor);    
  125. }
  126.  
  127.  
  128. function validarsolon(e){
  129.     obj=e.srcElement || e.target;
  130.     tecla_codigo = (document.all) ? e.keyCode : e.which;
  131.     if(tecla_codigo==8)return true;
  132.     patron =/[0-9]/;
  133.     tecla_valor = String.fromCharCode(tecla_codigo);
  134.     control=(tecla_codigo==46 && (/[.]/).test(obj.value))?false:true
  135.     return patron.test(tecla_valor) &&  control;
  136.  
  137. }
  138.  
  139. function validarsolol(e){
  140.     obj=e.srcElement || e.target;
  141.     tecla_codigo = (document.all) ? e.keyCode : e.which;
  142.     if(tecla_codigo==8)return true;
  143.     patron =/^[a-zA-ZñÑáéíóúÁÉÍÓÚ]+$/;
  144.     tecla_valor = String.fromCharCode(tecla_codigo);
  145.     return patron.test(tecla_valor);
  146. }
  147.  
  148. Funcional::::::::::::::::::::::::::
  149.  
  150. Solo numeros
  151.  
  152. <input type='text' onkeypress='validate(event)' />
  153.  
  154. function validate(evt) {
  155.   var theEvent = evt || window.event;
  156.   var key = theEvent.keyCode || theEvent.which;
  157.   key = String.fromCharCode( key );
  158.   var regex = /[0-9]|\./;
  159.   if( !regex.test(key) ) {
  160.     theEvent.returnValue = false;
  161.     if(theEvent.preventDefault) theEvent.preventDefault();
  162.   }
  163. }
  164.  
  165.  
  166. Datepicker bug:
  167.  
  168. $(function() {
  169.  
  170.   $( "#jQueryValidateTest" ).validate();
  171.  
  172.   $( "[type=date]" ).datepicker({
  173.     onClose: function() {
  174.       $( this ).valid();
  175.     }
  176.   });
  177. });
  178.  
  179. ADD METHODS:
  180.  
  181. $.validator.addMethod("lettersonly", function(value, element) {
  182.  
  183.   return this.optional(element) || /^[a-z\s]+$/i.test(value);
  184.  
  185. }, "Solo ingrese letras");
  186.  
  187. $.validator.addMethod('selectnone',function(value, element) {
  188.  
  189.   return this.optional(element) || (value!='seleccione');
  190.  
  191. }, "Seleccione una opcion");
  192.  
  193. $.validator.addMethod("morethan0", function(value, element) {
  194.  
  195.   return this.optional(element) || (parseFloat(value) > 0);
  196.  
  197. }, "* El monto debe ser mayor a 0");
  198.  
  199. $.validator.addMethod("formatfecha", function(value, element) {
  200.  
  201.   return value.match(/^(\d{4}\-\d{1,2}\-\d{1,2})$/);
  202.  
  203. }, "Por favor ingrese un formato valido");
Advertisement
Add Comment
Please, Sign In to add comment