Created
May 16, 2013 08:53
-
-
Save BlackPrincess/5590366 to your computer and use it in GitHub Desktop.
デフォルトで入力補助をするjs
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
$("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