Created
September 12, 2013 16:05
-
-
Save Gendrop/6539998 to your computer and use it in GitHub Desktop.
JavaScript: Validaciones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AaEeIiOoUuNn | |
var reTxt, rePwd, reUser, reNum, reDedimal, reEmail, reCURP, reDate, errorColor; | |
reTxt = /^[ A-Za-z0-9-_#.()\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00D1\u00F1\u00DC\u00FC]*$/; | |
rePwd= /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,30}$/; | |
reUser = /^[A-Za-z0-9_.]*$/; | |
reNum = /^[0-9]*$/; | |
reDedimal = /^\d+(\.\d{1,2})?$/; | |
reEmail = /^\w[a-zA-Z._]+@[a-zA-Z_]+?(\.[a-zA-Z]{2,3}){1,3}$/; | |
reCURP = /^[a-zA-Z]{4}\d{6}[a-zA-Z]{6}[a-zA-Z0-9]{2}$/; | |
reDate = /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/; | |
errorColor = "#FF0000"; | |
function requerido(campo){ | |
if (campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaTexto(campo){ | |
if (!campo.value.match(reTxt) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaPassword(campo){ | |
if (!campo.value.match(rePwd) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
alert("Debe contener almenos 6 d\u00edgitos. May\u00fasculas, min\u00fasculas y n\u00fameros"); | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaUsuario(campo, changeCase){ | |
if(changeCase){ | |
campo.value = campo.value.toLowerCase(); | |
} | |
if (!campo.value.match(reUser) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaNumeroLongitud(campo, len, showMessage){ | |
if (!campo.value.match(reNum) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else if(campo.value.length != len){ | |
campo.style.borderColor=errorColor; | |
if (showMessage) { | |
alert("Debe contener "+len+" d\u00edgitos"); | |
} | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaNumero(campo){ | |
if (!campo.value.match(reNum) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaCorreo(campo, changeCase){ | |
if(changeCase){ | |
campo.value = campo.value.toLowerCase(); | |
} | |
if (!campo.value.match(reEmail) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaSelect(campo, diferente){ | |
if (campo.value==diferente){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaRadio(campo, showMessage){ | |
if (campo[0].checked === false && campo[1].checked === false) { | |
campo[0].style.border="1px solid red"; | |
campo[1].style.border="1px solid red"; | |
return false; | |
}else{ | |
campo[0].style.border="1px solid white"; | |
campo[1].style.border="1px solid white"; | |
if(showMessage){ | |
alert('Debe seleccionar una opci\u00F3n'); | |
} | |
return true; | |
} | |
} | |
function validaNombre(campo, changeCase){ | |
if(changeCase){ | |
campo.value = toTitleCase(campo.value); | |
} | |
if (!campo.value.match(reTxt) || campo.value===null || campo.value==="" || campo.value.length < 3){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaCURP(campo, changeCase){ | |
if (changeCase) { | |
campo.value = campo.value.toUpperCase(); | |
} | |
if (!campo.value.match(reCURP) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaFecha(campo){ | |
if (!campo.value.match(reDate) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaFechaEnRango(campo, desde, hasta, mensaje){ | |
if (!campo.value.match(reDate) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else if(campo.value < desde.value || campo.value >= hasta.value){ | |
if(mensaje && campo.value > '1880-01-01'){ | |
alert('Fecha no corresponde al rango'); | |
} | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaMonto(campo){ | |
if (!campo.value.match(reDedimal) || campo.value===null || campo.value===""){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function validaCelular(campo){ | |
if(!validaNumero(campo) || campo.value.length != 10){ | |
campo.style.borderColor=errorColor; | |
return false; | |
}else{ | |
campo.style.borderColor=""; | |
return true; | |
} | |
} | |
function toTitleCase(str){ | |
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment