基于OpenLayers的WebGIS程序二次开发实例教程
34457 人在学
正则表达式,又称规则表达式。(英语:RegularExpression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
利用正则表达式校验是否为小数或者整数,废话不多说直接上demo(此正则表达式无法校验负数和数字为00开头的数字)。
PS:(如果有不对之处,请批评指教)
具体的编程代码如下所示:
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>checkNumber</title>
</head>
<bodystyle="background-color:aliceblue;">
<divid="main"style="margin:0auto;text-align:center;">
<formmethod="POST"action=""
style="width:50%;height:50%;text-align:center;margin:0auto;padding-top:20%;">
<label>請輸入判斷的數字:</label><inputtype="text"id="amount"name="amount"
onblur="checkAmount('amount','hint')"/><br/>
<p><spanname="hint"id="hint"style="color:red"></span></p>
</form>
</div>
</body>
<scriptsrc="https://code.jQuery.com/jquery-3.1.1.min.js"></script>
<scripttype="text/Javascript">
$(function(){
//設置div高度
$("#main").css("height",$(document).height);
$("#main").css("width",$(document).width);
});
functionnotIntOrDecimal(text){
//校驗(小數/數字)正則表達式
letpattern=/^[0-9]+([.]{1}[0-9]+){0,1}$/;
if(pattern.test(text)){
returnfalse;
}else{
returntrue;
}
}
functioncheckAmount(objName,hintName){
letjudgeVal=$("#"+objName).val();
if(judgeVal===''){
$("#"+hintName).html("請輸入一個校驗的數字!");
return;
}
if(judgeVal!==''&¬IntOrDecimal(judgeVal)){
$("#"+hintName).html("请输入一个整数或小数!");
$("#"+objName).focus();
}else{
$("#"+hintName).html("校驗成功!");
}
}
</script>
</html>
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成"regex",单数有regexp、regex,复数有regexps、regexes、regexen。