<body>
<div>
<form onsubmit="displaySum(); return false">
<h1>Sum of two numbers</h1>
<br>
<input id="number1" class="read" type="text" pattern="^[-\d]\d*$" required placeholder="Enter number 1">
<input id="number2" class="read" type="text" pattern="^[-\d]\d*$" required placeholder="Enter number 2">
<button id="submit-button" type="submit">Add</button>
<h1 id="displayArea"></h1>
</form>
</div>
<script>
function displaySum()
{
let num1=parseInt( document.getElementById("number1").value );
let num2=parseInt( document.getElementById("number2").value );
let sum = num1 + num2;
console.log("Sum is: "+ sum);
document.getElementById("displayArea").innerHTML=sum;
}
</script>
</body>
I got this error message:
Pattern attribute value ^[-\d]\d*$ is valid with the RegExp u
flag, but not with the v
flag: Uncaught SyntaxError: Invalid regular expression: /^[-\d]\d*$/v: Invalid character in character class. See https://crbug.com/1412729
I wanted to read two integers and output its sum on the screen and console. I tried matching pattern to read integer values and I got the output right. But I'm stuck with the error message.