Regular expression - integer check not working

I have a simple regex check problem for an integer value. I found the following on this site:

<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 a field, and then click outside the field, it says not an integer. Tried Firefox and Chrome.

+4
source share
1 answer

Syntax error:

 var intRegex = /^\d+$/; -^- 

\d means number. If you do not avoid this, it means literally d . More details here http://www.regular-expressions.info/

+12
source

Source: https://habr.com/ru/post/1479864/


All Articles