實數(shù)正則表達式怎么寫?哪位大神知道?我的正則表達式寫0.還是會通過驗證
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?//獲取第一個輸入框的值
? ? ? ? var txt1 = document.getElementById("txt1").value; ?
//獲取第二個輸入框的值
var txt2 = document.getElementById("txt2").value;?
//獲取選擇框的值
var sign = document.getElementById("select").value;?
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
var regex="^[0-9]*([\.]{0,1}[0-9]*)$";
//正浮點數(shù)正則
//var regex = "[1-9]\d*.\d*|0.\d*[1-9]\d*";
if(!txt1.match(regex) || !txt2.match(regex)){
? ?alert("請輸入數(shù)字!");
}else{
var result;
switch(sign){
? ?case "+":result=parseFloat(txt1)+parseFloat(txt2);
? ? ? ?break;
? ?case "-":result=parseFloat(txt1)-parseFloat(txt2);
? ? ? ?break;
? ?case "*":result=parseFloat(txt1)*parseFloat(txt2);
? ? ? ?break;
? ?case "/":result=parseFloat(txt1)/parseFloat(txt2);
? ? ? ?break;
? ?default:alert("無效運算!");
? ? ? ?break;
}
? ? //設置結果輸入框的值?
? ? document.getElementById("fruit").value=result;
}
? ?}
? </script>?
?</head>?
?<body>
? ?<input type='text' id='txt1' />?
? ?<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
? ?</select>
? ?<input type='text' id='txt2' />?
? ?<input type='button' value=' = ' onclick="count()"/> <!--通過 = 按鈕來調用創(chuàng)建的函數(shù),得到結果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
2018-06-22