利用正则表达式校验是否为小数或者整数,废话不多说直接上demo(此正则表达式无法校验负数和数字为00开头的数字)。

PS:(如果有不对之处,请批评指教)

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>checkNumber</title>
</head> <body style="background-color: aliceblue;">
<div id="main" style="margin: 0 auto; text-align: center; ">
<form method="POST" action=""
style="width: 50% ; height: 50%; text-align: center;margin: 0 auto; padding-top: 20%;">
<label>請輸入判斷的數字:</label><input type="text" id="amount" name="amount"
onblur="checkAmount('amount','hint')" /><br />
<p><span name="hint" id="hint" style="color: red"></span></p>
</form>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(function () {
//設置div高度
$("#main").css("height", $(document).height);
$("#main").css("width", $(document).width);
}); function notIntOrDecimal(text) {
//校驗(小數/數字)正則表達式
let pattern = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
if (pattern.test(text)) {
return false;
} else {
return true;
}
} function checkAmount(objName, hintName) {
let judgeVal = $("#" + objName).val();
if (judgeVal === '') {
$("#" + hintName).html("請輸入一個校驗的數字!");
return;
}
if (judgeVal !== '' && notIntOrDecimal(judgeVal)) {
$("#" + hintName).html("请输入一个整数或小数!");
$("#" + objName).focus();
} else {
$("#" + hintName).html("校驗成功!");
}
}
</script> </html>

最新文章

  1. Web AppBuilder Widget使用共享类库的方式
  2. Struts2版本配置2.1以前与以后---关于filter的配置
  3. 廖雪峰js教程笔记 1
  4. objective-c第六章课后练习5
  5. 《笨办法学python第三版》习题26,原错误代码及正确代码
  6. ping: unknown host www.baidu.com
  7. 4.抽象工厂模式(Abstract Factory)
  8. mount命令
  9. C++数据结构之Queue(队列)
  10. yii 创建步骤
  11. C# 获取中文星期的两种方法
  12. 18_高级映射:一对一查询(使用resultMap)
  13. PHP 常用命令
  14. Failed dependencies: 检查依赖性错误 解决方法
  15. 个人作业3-(Alpha阶段)
  16. Linux 和Windows之间命令行实现目录或文件互传
  17. 01Design and Analysis Algorithm Using Python-程振波
  18. nginx ssl 卸载功能 第八章
  19. h5设计图尺寸
  20. Linux c 从文件当中读取任意一行的数据

热门文章

  1. 详解cocos2dx 3.0的release版本在android平台的签名过程
  2. python3.7--pycharm selenium自启360浏览器/360极速浏览器方法
  3. bootstrap editable初始化后表单
  4. 一个强大的json解析工具类
  5. tensorflow学习——调试ctc的两个bug
  6. CentOS7 如何挂载网络设备
  7. 阶段3 1.Mybatis_04.自定义Mybatis框架基于注解开发_2 回顾自定义mybatis的流程分析
  8. IDEA maven 阿里云镜像 下载jar失败
  9. RequestContextHolder
  10. HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints