Created
December 2, 2010 13:38
-
-
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
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
/* 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