I have a simple problem of checking a regular expression for integer value. I found from this site the following:
<HTML>
<HEAD>
<script language="JavaScript">
function check_integer(el) {
var intRegex = /^d+$/;
var num=el.value;
if(!intRegex.test(num)) {
alert('must be an integer.');
}
}
</script>
</head>
<body>
<form>
<input type="text" name="f" onchange="check_integer(this)">
</form>
</body></html>
If I type 5 in the box and then click outside the box, it says not an integer. Tried on Firefox and Chrome.
Syntax error:
var intRegex = /^\d+$/;
-^-
\d
means "digit". If you don't escape it, it means literally d
. Learn more here http://www.regular-expressions.info/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With