Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active December 17, 2019 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveosoule/4aa1b83d1cdd9eefae24c9a23bccf145 to your computer and use it in GitHub Desktop.
Save steveosoule/4aa1b83d1cdd9eefae24c9a23bccf145 to your computer and use it in GitHub Desktop.
Miva - Field Validation in JavaScript
/*
/mm5/5.00/lib/util_public.mv
<MvFUNCTION NAME = "Phone_Validate" PARAMETERS = "phone, country" STANDARDOUTPUTLEVEL = "">
*/
function validate_phone(phone, country){
if( country === 'US' || country === 'DE' ){
phone = String(phone).replace(/[\+\/\(\)\-\x\.\s]/g, '');
if ( !/^\d+$/.test(phone) ) {
return false;
}
}
return phone.length > 0;
}
/*
/mm5/5.00/lib/util_public.mv
<MvFUNCTION NAME = "Zip_Validate" PARAMETERS = "zip, country" STANDARDOUTPUTLEVEL = "">
*/
function validate_zip(zip, country){
if ( !String(zip).length ) {
return false;
}
if (country === 'US') {
return /^\d{5}(-\d{4})?$/.test(zip);
}
if (country === 'DE') {
return /^\d{5}$/.test(zip);
}
return true;
}
/**
/mm5/5.00/admin/validate.mv
<MvFUNCTION NAME = "Validate_Password" PARAMETERS = "settings var, password" STANDARDOUTPUTLEVEL = "">
*/
const DOMAIN_PW_COMPLEX = &mvtj:global:Domain:pw_complex; // 1
const DOMAIN_PW_MIN_LENGTH = &mvtj:global:Domain:pw_min_len; // 7
function validate_password(password, min_length, complexity){
password = String(password);
if( password.length < Number(min_length) ){
return {
result: false,
message: 'Please enter a password that is greater than ' + min_length
}
}
if( !complexity ){
// No Complexity Requirements
return {
result: true
}
}
var has_upper = /[A-Z]/.test(password),
has_lower = /[a-z]/.test(password),
has_letter = has_upper || has_lower,
has_number = /[0-9]/.test(password),
has_punctuation = /\W/.test(password);
if( complexity === 1 ){
// At Least one Letter and one Number or Punctuation Character
if( has_letter && (has_number || has_punctuation) ){
return {
result: true
}
} else {
return {
result: false,
message: 'Please enter a password that has at Least one Letter and one Number or Punctuation Character'
}
}
}
else if ( complexity === 2 ){
// Mixed Case Letters and at Least one Number or Punctuation Character
if( has_upper && has_lower && (has_number || has_punctuation) ){
return {
result: true
}
} else {
return {
result: false,
message: 'Please enter a password that has mixed Case Letters and at Least one Number or Punctuation Character'
}
}
}
return {
result: true
}
}
/**
/mm5/5.00/lib/util_public.mv
<MvFUNCTION NAME = "Email_Validate" PARAMETERS = "address" STANDARDOUTPUTLEVEL = "">
*/
function validate_email(email){
if( /\s/.test(email) || /\.\./.test(email) ){
return false;
}
if( /@/.test(email) ){
if( /.+@[a-z].+[a-z]{2}/i.test(email) ){
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment