0

For some reason I'm having problems making this work with HTML5 form validation pattern attribute. I'm trying to match this condition /[!?^$\\\/{}]/g. I have tried escaping \ removing the \ with the global attribute, removing the [], almost everything. So there's always a first time to post a question on stackoverflow.

In a nutshell the user shouldn't input the following \/${}

<form action="#">
    <input type="text" required pattern="/[!?^$\\\/{}]/g">
    <button type="submit">Submit</button>
</form>

4.20 - 4.21 New export -> Should be valid

4/20 - 2/21 $New {export} -> Should be invalid

Here's the fiddle: http://jsfiddle.net/TV28A/

6
  • 2
    What strings are you validating this regex against? Commented May 2, 2014 at 16:58
  • I don't think you can use the /.../g format SO - Click here Commented May 2, 2014 at 16:59
  • You mean as an example? If the user does 4/20 - 4/21 $Date That should be rejected. It should accept all string characters except \/${} @GregBurghardt Commented May 2, 2014 at 16:59
  • @JasonWilczak tried that already and it didn't worked Commented May 2, 2014 at 17:01
  • can you provide an example of a string that you want to to be considered valid and one that you don't? Commented May 2, 2014 at 17:05

2 Answers 2

2

I have tried all the answers above and any worked (don't know if I am testing wrong). But Just in case:

I think if you want to block only the \/${} characters, this should be the right regex [^\/\\${}]*

so the code would be something like:

<form action="#">
    <input type="text" required pattern="[^\/\\${}]*">
    <button type="submit">Submit</button>
</form>

Putting the ^ character as the first one into the brackets, it negate the sentence, so [^\/\\${}]* means string which do NOT have / \ $ { } of any size.

EDIT:

Sharing the jsFiddle as requested in the comments.

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

2 Comments

Can you show a jsFiddle where this works. I can't get it to work.
posted jsFiddle as requested @jfriend00
0

There is no need for you to enclose your RegEx in / when using the pattern attribute. Furthermore, the g flag is also unnecessary due to the fact that the expression must match the entire input field, rather than just a section of it.

This should suffice for your purposes:

<form action="#">
  <input type="text" required pattern="[!?^$\\\/{}]">
  <button type="submit">Submit</button>
</form>

4 Comments

Did you guys try this with the jsfiddle link? Doesn't seem to be working. cc: @jfriend00
this doesn't validate OP's strings as desired FIDDLE
@JasonWilczak I must be going crazy but your Fiddle is not working for me.
Yeah, my point was to show that the answer wasn't working based on the Fiddle i provided, sorry about that :)

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.