1

I'm currently using a regex to detect space, single-quote & double-quote in an input.

I would like to add the detection of several strings: ex. 'xxx' & 'yyy'. It should only detect for those complete strings, so 'x' or 'y' by itself wouldn't be detected.

Currently have this in html input code:

<input type="text" pattern="^(?=.*[\x27\x20\x22]).+$" ...
2
  • 1
    pattern="^(?=.*[\x27\x20\x22]).*(xxx|yyy).*$"? Commented Jan 26, 2019 at 22:57
  • @WiktorStribiżew I tried it - the xxx & yyy is working but now the space/'/" isn't working. Commented Jan 26, 2019 at 23:16

1 Answer 1

1

If you just want to ensure exactly one space, single-quote or double-quote appears in the input, you can simplify your pattern to:

<input type="text" pattern="[\x27\x20\x22]" ...

To detect other things, you can add "alternations", separated by |:

<input type="text" pattern="[\x27\x20\x22]|xxx|yyy" ...

As per jhnc: To detect any instance, no matter where in the string:

<input type="text" pattern=".*([\x27\x20\x22]|xxx|yyy).*" ...
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry to bother you, but I tried your pattern and it's not working - I've set up a jsfiddle - hopefully you can tell me what's wrong. Test by changing value="" to something jsfiddle.net/billparti/x0t7k5sn
What are you expecting to happen? If I delete the value="xxx" and all the CSS, run, and just type into the textbox, I see the expected browser highlighting.
By the way, if you want to match something like "hello xxx.123" and not precisely " " or "xxx", etc, then you will need to put back the .* at the start and end: eg. pattern=".*([\x27\x20\x22]|xxx|yyy).*" as patterns are implicitly wrapped with ^ and $
If you mean that "Pattern Match" is never displayed, it's because the .warn div is not a child of input. Try changing the css selector to ... ~ .warn or ... + .warn or somesuch. cf. w3schools.com/cssref/css_selectors.asp
Yep, I needed the additional code ( the third suggestion) is now the accepted answer - thanks

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.