[新着] Webテンプレートを仮オープンしました
<html>
<head>
<title>数値かどうか調べる</title>
<script type="text/javascript">
<!--
function check(){
str = document.F1.num.value;
data = str.match(/[^0-9\-]/g);
if (data) alert("半角数字以外の "+data+" が含まれています");
}
// -->
</script>
</head>
<body>
半角数字と-(ハイフォン)だけ入力可
<form name="F1">
<input type="text" name="num" onKeyDown="check()">
<input type="button" value="リセット" onClick="reset()">
</form>
</body>
</html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Untitled-Doc</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function check(){
if(isValid(document.f1.number.value) == false){
alert("有効な入力ではありません")
return false;
}
}
function isValid(num){
var i,num2;
var checkStr = new String('0123456789-');
num2 = num.replace(/ /g,'');
if(num2.length == 0){
return false;
}
for(i=0;i<num2.length;i++){
if(checkStr.indexOf(num2.charAt(i),0) == -1){
return false;
}
}
return true;
}
</script>
</head>
<body>
<form name="f1">
<input type="text" name="number">
<input type="button" value="Check" onClick="check()">
</form>
</body>
</html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
function numberOnly() {
str = String.fromCharCode( event.keyCode );
if ( "0123456789\b\r\t".indexOf( str, 0 ) < 0 ) {
return false;
}
return true;
}
-->
</script>
</head>
<body>
<form>
<input type="text" name="number" onkeydown="return numberOnly()">
</form>
</body>
</html>