最も簡単なJavaScript検証整数、小数、実数、有効ビット小数正規表現



<!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> 
<title>                </title> 
</head> 
<body> 
<h3>           !(      !)</h3> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^/d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-/d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^/d+/./d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-/d+/./d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+/./d+$/.test(this.value));" /> 
<br /> 
   :  <input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d+)?$/.test(this.value));" /> 
<br /> 
  1   :<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,1})?$/.test(this.value));" /> 
<br /> 
  2   :<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,2})?$/.test(this.value));" /> 
<br /> 
  3   :<input type="text" size="20" onkeydown="if(event.keyCode == 13) alert(/^-?/d+(/./d{1,3})?$/.test(this.value));" /> 
<br /> 
</body> 
</html>