JavaScript Regexp Regular Expression
<!DOCTYPE html>
<html>
<body>
<p>The test() method returns true if it finds a match, otherwise it returns false.</p>
<p>Click the button to search a string for the character "e".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "-2:59";
var patt = /^-?\d*:[0-5][0-9]/;
var res = patt.test(str);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html> .test() method will return true if format is correct and false if format is wrong.
var patt = /^-?\d*:[0-5][0-9]/;
^- Means optional of -
\d* means any digit of decimal
\d Means only one digit
: Means compulsory to put :
^: Means optional to put :
[0-5] means only can enter number 0-5 only.
[0-9] means only can enter number 0-9 only.
Above formula is to force user enter time format in positive or negative.
Try the code here:
https://www.w3schools.com/code/tryit.asp?filename=GKJ2YWOF31DB
Comments
Post a Comment