Skip to content

Instantly share code, notes, and snippets.

@BlackPrincess
Created May 16, 2013 08:53
Show Gist options
  • Save BlackPrincess/5590366 to your computer and use it in GitHub Desktop.
Save BlackPrincess/5590366 to your computer and use it in GitHub Desktop.
デフォルトで入力補助をするjs
$("input[data-type=integer]").on("blur", function(){
var value = $(this).val();
if(!$.isNumeric(value)) {
$(this).val("");
return;
}
$(this).val(Math.floor(value));
});
$("input[data-type=float]").on("blur", function(){
var value = $(this).val();
if(!$.isNumeric(value)) {
$(this).val("");
return;
}
var digit = $(this).attr("data-float-digit");
if(!$.isNumeric(digit) || digit == "") {
digit = 2; // 初期値として二桁
}
value *= Math.pow(10, digit);
value = Math.floor(value) / Math.pow(10, digit);
$(this).val(value);
});
$("input[data-type=date]").on("blur", function(){
var value = $(this).val();
var date = new Date(value);
if(date.toString() == "Invalid Date") {
$(this).val("");
return;
}
var format = $(this).attr("data-date-format");
if(!format || format === "") {
format = "yyyy/MM/dd"; // datetimeで作った方がよかった?
}
$(this).val(date.toFormatString(format));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment