1
<input type="text" name="address" pattern="[0-9a-zA-Z ,\.\/\-&']{0,}">

It will allow all except of few special characters but i don't want '//' '///' or any special character repetition like this. what should i change ?

0

2 Answers 2

2

You need to re-write the pattern as

pattern="(?:[0-9a-zA-Z]*(?:[ ,./&'-][0-9a-zA-Z]+)*)?"

See JS demo:

<form>
<input type="text" name="address" pattern="(?:[0-9a-zA-Z]*(?:[ ,./&'-][0-9a-zA-Z]+)*)?">
<input type="submit">
</form>

Note that ^ and $ are added by default, but in case you are using some additional frameworks that override the pattern attribute, you need to add the anchors to the pattern explicitly:

^(?:[0-9a-zA-Z]*(?:[ ,./&'-][0-9a-zA-Z]+)*)?$

Details:

  • ^ - start of a string
  • (?:[0-9a-zA-Z]*(?:[ ,./&'-][0-9a-zA-Z]*)*)? - an optional non-capturing group matching 1 or 0 occurrences of:
    • [0-9a-zA-Z]* - zero or more ASCII letters/digits
    • (?:[ ,./&'-][0-9a-zA-Z]*)* - 0 or more occurrences of:
      • [ ,./&'-] - a space, ,, ., / (no need to escape it!), &, ' or - (no need to escape it at the end of the character class, but you can)
      • [0-9a-zA-Z]+ - one or more ASCII letters/digits
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

1

You can add a negative lookahead to check repetition :
(?!.*([ ,./&'-])\1)[0-9a-zA-Z ,./&'-]*
This will reject strings with a space, ,, ., /, &, ', or - repeated.

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.