Skip to content

Instantly share code, notes, and snippets.

@adamhunter
Created December 2, 2010 13:38
Show Gist options
  • Save adamhunter/725306 to your computer and use it in GitHub Desktop.
Save adamhunter/725306 to your computer and use it in GitHub Desktop.
these are js autocleaners for attributes we'll be validating with a regexp server side
/* autocleaning attributes */
var cleaner = {};
cleaner.phone = function(){
var regex = /[^0-9x]/g;
var value = $(this).val();
if (regex.test(value)) {
$(this).val(value.replace(regex, ''));
}
};
cleaner.date = function(){
var regex = /[^0-9\-]/g;
var value = $(this).val();
value = value.replace(/\/|\s/g, '-');
value = value.replace(/^(\d{2})$|^(\d{2}-\d{2})$/, "$1$2-");
value = value.replace(/^(\d{1})-/, "0$1-");
value = value.replace(/^(\d{2})-(\d{1})-/, "$1-0$2-");
value = value.replace(regex, '');
$(this).val(value.replace(/(\-\-)+/g, '-'));
};
$('.autoclean.phone').live('blur', cleaner.phone).live('keyup', cleaner.phone);
$('.autoclean.date').live('blur', cleaner.date).live('keyup', cleaner.date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment