Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression - check for integer does not work

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.

like image 599
Ray S. Avatar asked Dec 12 '22 15:12

Ray S.


1 Answers

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/

like image 136
elclanrs Avatar answered Dec 29 '22 21:12

elclanrs