<html> <body> <input type="text" onblur="checknum(this)"/> <script type="text/javascript"> function checknum(obj){ if(obj.value.match(/^[1-9]\d*$/)==null){ obj.style.color = "red"; } else { obj.style.color = "black"; } } </script> </body> </html>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script type="text/javascript"> <!-- function isNum() { var txtField = document.getElementById('txt'); var txt = txtField.value; var regexp = /^\d*$/; if (regexp.test(txt)) { txtField.style.color = "#000"; } else { txtField.style.color = "#f00"; } } //--> </script> </head> <body> <input id="txt" type="text" onKeyUp="isNum();" /> </body> </html>不知道合不合要求,新手一枚哈
Input: <input class="positive_integer" type='number' min='1' required>
.input_error { border-style: solid; border-color: red; }
$(function() { $("input.positive_integer").on("keyup change", function(event) { //check input number var val = $(this).val(); if(val != '') { if(!is_positive_number(val)) { $(this).addClass("input_error"); } else { $(this).removeClass("input_error"); } } else { $(this).removeClass("input_error"); } }); function is_positive_number(num) { console.log(num); if(num == 0) { return 0; } if(num && /^\d+$/.test(num)) { return 1; } else { return 0; } } });
<!doctype html>
<html>
<head>
<script type="text/javascript">
function isInt() {
var regexp = /^\d*$/;
var inputField = document.getElementById('input');
if (!regexp.test(inputField.value)) {
inputField.style.color='red';
} else {
inputField.style.color='black';
}
}
</script>
<style type="text/css">
#input {
position: absolute;
width: 40%;
height: 6%;
top:47%;
left:30%;
padding: 9px 7px;
padding: 11px 7px 7px\9;
font: 16px arial;
border: 1px solid #d8d8d8;
border-bottom: 1px solid #ccc;
vertical-align: center;
outline: none;
}
</style>
</head>
<body>
<input id="input" type="text" oninput="isInt();" />
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script
type="text/javascript" >
function isum(){
var text=document.getElementById("aa");
var ts=
/^\d*$/;
if(!ts.test(text.value)){
text.style.color='red';
}
else{
text.style.color='green';
}
}
</script>
</head>
<body>
<input type="text"
id="aa" onblur="isum()"/>
</body>
<input type="text" id="testInput" onkeyup="test()">JS:
var text = document.getElementById('testInput'); var textValue = text.value; var reg = /^(0|\+?[1-9][0-9]*)$/; function test(){ if(reg.test(textValue)){ text.style.color = 'red'; } else { text.style.color = 'black'; } }
<!DOCTYPE html> <html> <head> <title>demo</title> </head> <body> <input type="text" id="digit" placeholder="input a number"/> <script> (function() { var input = document.getElementById("digit"); input.addEventListener('keyup',function(e){ val = e.target.value; console.log(val); pattern = /^[0-9]*$/; if (pattern.test(val)) { e.target.style.color = "inherit"; } else { e.target.style.color = "#f00"; } },false); })(); </script> </body> </html>