We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

MartinY on 07/13/07


Tagged


Versions (?)


Funciones varias JavaScript


Published in: JavaScript 


  1. function activarDiv (numero, elementos) {
  2. /* en caso de no pasarle numero se pone 0 por defecto */
  3. if ((numero == null) || (numero == '')) numero = 0;
  4. if (numero > elementos[1].length-1) numero = 0;
  5. for (i=0;i<elementos[1].length;i++){
  6. if (numero == i) {
  7. $(elementos[1][i]).style.display = "";
  8. $(elementos[2][i]).className = "activo";
  9. }
  10. else {
  11. $(elementos[1][i]).style.display = "none";
  12. $(elementos[2][i]).className = "";
  13. }
  14. }
  15. $(elementos[0]).value = numero;
  16. }
  17.  
  18. function mostrarZona(zona,mostrar) {
  19. if ($(zona))
  20. if (mostrar) $(zona).style.display = "";
  21. else $(zona).style.display = "none";
  22. else
  23. alert('La zona ('+zona+') a mostrar no existe');
  24. }
  25.  
  26. Array.prototype.inArray = function (value) {
  27. var i;
  28. for (i=0; i < this.length; i++) {
  29. if (this[i] == value) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. Array.prototype.indexOf = function(s) {
  36. for (var x=0;x<this.length;x++) if(this[x] == s) return x;
  37. return false;
  38. }
  39. String.prototype.trim = function() { return this.replace(/^s+|s+$/g,'') }
  40. String.prototype.zeroFill = function(n) {
  41. var d=this;
  42. for(var x=this.length;x<n;x++)
  43. d='0'.concat(d);
  44. return d;
  45. }
  46. // rellenar string con ceros
  47. function zeroFill(o,n) {
  48. for(var x=o.value.length;x<n;x++)
  49. o.value='0'+o.value
  50. }
  51.  
  52. Object.prototype.selectedValue = function(v) {
  53. if (this.type != 'select-one') return;
  54. for(var x=0;x<this.options.length;x++){
  55. if(this.options[x].value == v){
  56. this.selectedIndex = x;
  57. return;
  58. }
  59. }
  60. }
  61. // selecciona un valor de un SELECT
  62. function seleccionarValor(o,v) {
  63. for(var x=0;x<o.options.length;x++){
  64. if(o.options[x].value == v){
  65. o.selectedIndex = x;
  66. return;
  67. }
  68. }
  69. }
  70.  
  71. /* get, set, and delete cookies */
  72. function getCookie( name ) {
  73. var start = document.cookie.indexOf( name + "=" );
  74. var len = start + name.length + 1;
  75. if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
  76. return null;
  77. }
  78. if ( start == -1 ) return null;
  79. var end = document.cookie.indexOf( ";", len );
  80. if ( end == -1 ) end = document.cookie.length;
  81. return unescape( document.cookie.substring( len, end ) );
  82. }
  83.  
  84. function setCookie( name, value, expires, path, domain, secure ) {
  85. var today = new Date();
  86. today.setTime( today.getTime() );
  87. if ( expires ) {
  88. expires = expires * 1000 * 60 * 60 * 24;
  89. }
  90. var expires_date = new Date( today.getTime() + (expires) );
  91. document.cookie = name+"="+escape( value ) +
  92. ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
  93. ( ( path ) ? ";path=" + path : "" ) +
  94. ( ( domain ) ? ";domain=" + domain : "" ) +
  95. ( ( secure ) ? ";secure" : "" );
  96. }
  97.  
  98. function deleteCookie( name, path, domain ) {
  99. if ( getCookie( name ) ) document.cookie = name + "=" +
  100. ( ( path ) ? ";path=" + path : "") +
  101. ( ( domain ) ? ";domain=" + domain : "" ) +
  102. ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  103. }
  104.  
  105. /* quick getElement reference */
  106. function $() {
  107. var elements = new Array();
  108. for (var i = 0; i < arguments.length; i++) {
  109. var element = arguments[i];
  110. if (typeof element == 'string')
  111. element = document.getElementById(element);
  112. if (arguments.length == 1)
  113. return element;
  114. elements.push(element);
  115. }
  116. return elements;
  117. }
  118.  
  119. function validarFecha(input_fecha){
  120. if (!/^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]$/.test(input_fecha.value)){
  121. alert("El formato de la fecha debe ser dd/mm/aaaa");
  122. input_fecha.value='';
  123. return;
  124. }
  125. valor = input_fecha.value.split('/');
  126. if(valor[0]>31 || valor[0]<1){
  127. alert("Día incorrecto");
  128. input_fecha.value='';
  129. }else{
  130. if(valor[1]>12 || valor[1]<1){
  131. alert("Mes incorrecto");
  132. input_fecha.value='';
  133. }else{
  134. if (valor[1]== 2 || valor[1]== 4 || valor[1]== 6 || valor[1]== 9 || valor[1]== 11){
  135. if(valor[0]>30){
  136. alert("Día incorrecto");
  137. input_fecha.value='';
  138. }
  139. }
  140. if(valor[1]== 2){
  141. anio = valor[2]%4;
  142.  
  143. if(anio!=0 && valor[0]>28){
  144. alert("Día incorrecto");
  145. input_fecha.value='';
  146. }else{
  147. if(valor[0]>29){
  148. alert("Día incorrecto");
  149. input_fecha.value='';
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157.  
  158. function validarNumero(numero){
  159. if (!/^([0-9])*$/.test(numero.value)){
  160. alert("El valor " + numero.value + " no es un número");
  161. numero.value='';
  162. }
  163. }
  164.  
  165. function validarTelefonoMovil(telefono) {
  166. if(!/^6[0-9]{8}$/.test(telefono.value)){
  167. alert("El valor " + telefono.value + " no es correcto");
  168. telefono.value = '';
  169. telefono.focus();
  170. }
  171. }
  172.  
  173. function validarTelefonoFijo(telefono) {
  174. if(!/^9[0-9]{8}$/.test(telefono.value)){
  175. alert("El valor " + telefono.value + " no es correcto");
  176. telefono.value = '';
  177. telefono.focus();
  178. }
  179. }
  180.  
  181. function validarEmail(email )
  182. {
  183. var filtro=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
  184. if (email.value.length == 0 ) return true;
  185. if (filtro.test(email.value))
  186. return true;
  187. else{
  188. alert("Ingrese una dirección de correo válida");
  189. email.value='';
  190. email.focus();
  191. return false;
  192. }
  193. }

Report this snippet 

You need to login to post a comment.