javascript - Check a String matches a Regex -
I want to use javascript to perform some client-side validation so that it can check whether the string matches the ragaks :
Is wrong in my JS? Data-lang = "js" data-hide = "wrong">
& lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Body & gt; & Lt; Button onclick = "myfunction ()" & gt; Test & lt; / Button & gt; & Lt; P id = "demo" & gt; & Lt; / P & gt; & Lt; Script & gt; Function myFunction () {var str = "30/12/2015"; Var pat = new regx ("DD / MM / YYYY"); Var res = patt.test (str); Document.getElementById ("Demo"). InnerHTML = res; } & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Html & gt;
This is not a regular expression The easiest (but extraordinary) solution will be:
var is datecorchatch = new date (str) .toString ()! == "Invalid date"; New date (ARR) will try to parse the date and if it fails, then it will return the object after which toString produces a string invalid date on it, so you got a basic date recognition.
Comments
Post a Comment