2

.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).* this pattern was working fine. But suddenly it stop working in chrome and opera lately. What's going on here ? What a problem is here and how it's wrong? Opera is informing about invalid escape, same in chrome. It works fine when im checking it in js.

<form>
<input type="text" pattern=".*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).*">
<button>
Send
</button>
</form>

5
  • Cna you show us how your running this regex ? Commented Oct 6, 2016 at 11:50
  • 4
    You don't need to escape the dashes (simply use -, not \-), that must be what you're warned about, and maybe the cause of dysfunction. Dashes only need to be escaped when used literally in a character class. Commented Oct 6, 2016 at 11:51
  • Updated in OP with code snippet Commented Oct 6, 2016 at 11:53
  • Also tell us the exact error message, not a paraphrase of it. Commented Oct 6, 2016 at 11:54
  • Actually @Aaron is correct. Just using - instead of \- works fine. Commented Oct 6, 2016 at 11:55

2 Answers 2

4

The point is that Chrome and Firefox already support ES6 regex specifications and support the Unicode mode by default.

Unicode patterns have stricter rules as to what characters can be escaped inside the pattern. See this reference:

IdentityEscape: In BMP patterns, many characters can be prefixed with a backslash and are interpreted as themselves (for example: if \u is not followed by four hexadecimal digits, it is interpreted as u). In Unicode patterns that only works for the following characters (which frees up \u for Unicode code point escapes): ^ $ \ . * + ? ( ) [ ] { } |

The same set of chars is referred to as SyntaxCharacter in the ES6 specs page.

So, you can only escape the - inside the character class where it is considered a special character and to make it a literal you can escape it. Everywhere else it must not be escaped.

<form>
  <input type="text" pattern=".*(\d{3}-\d{3}-\d{2}-\d{2}|\d{3}-\d{2}-\d{2}-\d{3}|\d{10}).*">
  <input type=Submit>
 </form>

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use below concept to implement to validate the date format

<form onsubmit="alert('Submitted.');return false;"><input required="" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}" value="" name="dates_pattern0" id="dates_pattern0" list="dates_pattern0_datalist" placeholder="Try it out." type="text"><input value="»" type="submit"></form>

you can find more validations by this link - http://html5pattern.com/Dates

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.