Skip to content

Instantly share code, notes, and snippets.

@mhhansen
Last active July 7, 2017 18:41
Show Gist options
  • Save mhhansen/0a88b39a70315d4b0d657cd6c570a39d to your computer and use it in GitHub Desktop.
Save mhhansen/0a88b39a70315d4b0d657cd6c570a39d to your computer and use it in GitHub Desktop.
JS validation - codigo postal, nro documento, telefono.
/**
* US phone format:
* - 1 followed by a digit different from 0, followed by 9 digits (18884622592)
* - a digit different from 0, followed by 9 digits (8884622592)
*/
function main(target){
return /(^[1]{0,1}[1-9]{1}[0-9]{9}$)/.test(target);
};
/**
* UY phone format:
* - mobile: 09, followed by a digit different from 0 followed by 6 digits
* - Montevideo: 2 followed by 7 digits
* - Uruguay (with the exception of Montevideo): 4 followed by 7 digits
*/
function main(target){
return /(^09[1-9]{1}[0-9]{6}$)|(^2[0-9]{7}$)|(^4[0-9]{7}$)/.test(target);
};
/**
* PY phone format:
* - 0 followed by a digit different from 0 followed by 7 to 10 digits
* - a digit different from 0 followed by 7 to 10 digits
*/
function main(target){
return /(^0[1-9]{1}[0-9]{7,10}$)|(^[1-9]{1}[0-9]{7,10}$)/.test(target);
};
/**
* AR phone format:
* - long distance call: 0 (optional)
* - area code: 2 to 4 digits
* - phone: 6 to 8 digits
* Mobile:
* - long distance call: 0 (optional)
* - area code: 2 to 4 digits
* - mobile indicator: 15
* - phone: 6 to 8 digits
*/
function main(target){
return /(^[0]{0,1}\d{8,12}$)|(^[0]{0,1}\d{0,4}15\d{6,8}$)/.test(target);
};
/**
* UY: cedula de identidad
*/
function main(target){
if (typeof target === 'undefined') {
return false;
}
/**
* The CI wust have 8 digits including the verification digit.
* If there are less than 8 digits, add 0 at the beginning
*/
if (target.length > 8) {
return false;
}
if (target.length < 8) {
for (var i = target.length; i < 8; i++) {
target = "0" + target;
}
}
var computation = 0;
computation = computation + (target[0] * 2);
computation = computation + (target[1] * 9);
computation = computation + (target[2] * 8);
computation = computation + (target[3] * 7);
computation = computation + (target[4] * 6);
computation = computation + (target[5] * 3);
computation = computation + (target[6] * 4);
var nextNumberEndingInZero = computation;
while ((nextNumberEndingInZero % 10) != 0) {
nextNumberEndingInZero = nextNumberEndingInZero + 1;
}
var calculatedVerificationDigit = nextNumberEndingInZero - computation;
var verificationDigit = target[7];
if (calculatedVerificationDigit == verificationDigit) {
return true;
}
return false;
}
/**
* UY: RUT
*/
function main(target){
return /(^[0-9]{12}$)/.test(target);
}
/**
* PY: cedula de identidad
*/
function main(target){
return /(^[0-9]{6,9}$)/.test(target);
}
/**
* PY: RUC
*/
function main(target){
return /(^[0-9]{5,9}$)/.test(target);
}
/**
* AR: DNI
*/
function main(target){
return /(^[0-9]{7,8}$)/.test(target);
}
/**
* AR: CUIT
*/
function main(target){
var aMult = '5432765432';
var aMult = aMult.split('');
if (target && target.length == 11)
{
aCUIT = target.split('');
var iResult = 0;
for(i = 0; i <= 9; i++)
{
iResult += aCUIT[i] * aMult[i];
}
iResult = (iResult % 11);
iResult = 11 - iResult;
if (iResult == 11) iResult = 0;
if (iResult == 10) iResult = 9;
if (iResult == aCUIT[10])
{
return true;
}
}
return false;
}
/**
* US phone format:
* - 1 followed by a digit different from 0, followed by 9 digits (18884622592)
* - a digit different from 0, followed by 9 digits (8884622592)
*/
function main(target){
return /(^[1]{0,1}[1-9]{1}[0-9]{9}$)/.test(target);
};
/**
* UY phone format:
* - mobile: 09, followed by a digit different from 0 followed by 6 digits
* - Montevideo: 2 folowed by 7 digits
* - Uruguay (with the exception of Montevideo): 4 folowed by 7 digits
*/
function main(target){
return /(^09[1-9]{1}[0-9]{6}$)|(^2[0-9]{7}$)|(^4[0-9]{7}$)/.test(target);
};
/**
* PY phone format:
* - 0 followed by a digit different from 0 followed by 7 to 10 digits
* - a digit different from 0 followed by 7 to 10 digits
*/
function main(target){
return /(^0[1-9]{1}[0-9]{7,10}$)|(^[1-9]{1}[0-9]{7,10}$)/.test(target);
};
/**
* AR phone format:
* - long distance call: 0 (optional)
* - area code: 2 to 4 digits
* - phone: 6 to 8 digits
* Mobile:
* - long distance call: 0 (optional)
* - area code: 2 to 4 digits
* - mobile indicator: 15
* - phone: 6 to 8 digits
*/
function main(target){
return /(^[0]{0,1}\d{8,12}$)|(^[0]{0,1}\d{0,4}15\d{6,8}$)/.test(target);
};
/**
* US postal code format:
* - 5 digits (33126)
* - 5 digits dash 4 digits (33126-5896)
*/
function main(target){
return /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(target);
};
/**
* UY postal code format:
* - 5 digits (33126)
*/
function main(target){
return /(^\d{5}$)/.test(target);
};
/**
* PY postal code format:
* - 4 digits (3326)
*/
function main(target){
return /(^\d{4}$)/.test(target);
};
/**
* AR postal code format:
* - 1 letra y 4 digits (B1636)
* - 1 letra, 4 digits y 3 letras (B1636FDA)
* Explicacion:
* - la primer letra identifica a la provincia
* - los cuatro digitos representa el codigo postal utilizado anteriormente
* - las tras letras del final identifican la cara de la manzana
*/
function main(target){
return /(^[a-zA-Z]{1}\d{4}$)|(^[a-zA-Z]{1}\d{4}[a-zA-Z]{3}$)/.test(target);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment