- <script type="text/javascript">
- function checkDecimals(){
- var decallowed = 2;
- var revValue = document.getElementById("rev").value;
- if(revValue.indexOf('.') == -1){
- revValue += ".";
- }
- var dectext = revValue.substring(revValue.indexOf('.')+1, revValue.length);
- if(dectext.length > decallowed){
- alert ("careful!,don't write more than " + decallowed + " decimal. please try again!");
- document.getElementById("rev").focus();
- return false;
- }else{
- return true;
- }
- }
- </script>
上面是JS的代码,下面是html的代码:
- <input type="submit" onclick="JavaScript:if(confirm('Are your sure to save it?')){return checkDecimals();}else{return false;}" value="save" />
解释上面的代码:
(1)本来调用的是delete的JS代码:它原本应该这样写
- <input type="submit" onclick="JavaScript:if(confirm('Are your sure to save it?')){return true;}else{return false;}" value="save" />
(2)但是现在要加判断里面输入框的验证,本来验证是这样写
- <input type="submit" onclick="return checkDecimals();" value="save" />
现在要把这两个代码合并起来用,所以在delete的JS代码里面就不能返回return true;了,要改成return checkDecimals(); 这样就可以解决上面的问题了。