このコードを使用できます
<form>
<input type="text" id='myField' />
<input type="submit" id="submit" value="Submit" />
</form>
<script>
var myField = document.getElementById('myField');
myField.addEventListener('input', function (prev) {
return function (evt) {
if (!/^\d+(?:\.\d{0,2})?$/.test(this.value)) {
this.value = prev;
this.style.borderColor = 'red';
this.style.fontWeight = 'bold';
document.getElementById('submit').disabled = true;
}
else {
prev = this.value;
this.style.borderColor = '';
this.style.fontWeight = '';
document.getElementById('submit').disabled = false;
}
};
}(myField.value), false);
</script>
(編集)
あなたのコメントに基づいて、複数のフィールドで機能する回答を編集しました。入力フィールドにクラス名を追加するだけで、javascript で処理できます
<form>
<input type="text" class='decimalField' />
<input type="text" class='decimalField' />
<input type="text" class='decimalField' />
<input type="submit" id="submit" value="Submit" />
</form>
<script>
var decimalField = document.getElementsByClassName('decimalField');
for(var i = 0; i<decimalField.length; i++){
decimalField[i].addEventListener('input', function (prev) {
return function (evt) {
if (!/^\d+(?:\.\d{0,2})?$/.test(this.value)) {
this.value = prev;
this.style.borderColor = 'red';
this.style.fontWeight = 'bold';
document.getElementById('submit').disabled = true;
}
else {
prev = this.value;
this.style.borderColor = '';
this.style.fontWeight = '';
document.getElementById('submit').disabled = false;
}
};
}(decimalField[i].value), false);
}
</script>