javascript - RegExp - needs to check there are letters and numbers present plus optional special chars? -


this question has answer here:

using javascript have test value ensure alphanumeric, can have selection of optional special chars - have:

[a-za-z0-9\/_\+£&@"\?!'\.,\(\)] 

but fails optional aspect of test.

e.g. these should valid:

alpha1234 alpha1234! 1234alpha !1234alpha 1234!qwer 

but these should fail:

alpha 1234 

hopefully can either point me in right direction or has answer handy :) or if need know more, let me know.

thanks in advance.

  • first, ensure input contains @ least 1 digit: (?=.*[0-9]).
  • then, ensure input contains @ least 1 alpha: (?=.*[a-z]).
  • finally, ensure input contains allowed chars: [a-z0-9\/_+£&@"?!'.,()]*.

all together:

^(?=.*[0-9])(?=.*[a-z])[a-z0-9\/_+£&@"?!'.,()]*$ 

regular expression visualization

visualization debuggex

demo regex101


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -