Search code examples
htmlregexvalidationinputbrowser

html input pattern attribute not working correctly


I writed input that should accept only actual digits for example: 1.2 - accepted 1.1.2 - not accepted +123.53e-10.1 - accepted etc.

here is full code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <input type="text" placeholder="Digit" pattern="\s*[-+]?\d+(\.\d+)?([eE][-+]?\d+(\.\d+)?)?\s*">

        <input type="submit" value="Submit">
    </form>
</body>
</html>

My code is working only in Internet Explorer. What do I need to do to make the code work properly in Chrome, FireFox, Opera etc.


Solution

  • Escape the signs + and - that are inside the square brackets:

    \s*[\-\+]?\d+(\.\d+)?([eE][\-\+]?\d+(\.\d+)?)?\s*
    ``