js検証小数または整数
13209 ワード
正規表現で小数または整数かどうかをチェックし、くだらないことを言わずに直接demoに行きます(この正規表現では負数と数字が00の先頭の数字はチェックできません).
PS:(間違いがあれば、批判して教えてください)
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>checkNumbertitle>
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>