write regex for everything except a particular pattern for password in javascript -
i'm using following regex pattern validating password.
var expression = /^[^\!\@\#\$\%\^\&\*\(\)\-\s\=\+\[\{\]\}\;\:\'\"\<\>\?\/\,\.\`\~\'][^\s]+$/
and it's working fine.
now need change in such way can give characters allowed or blocked on top of this.
i tried following code not working.
var blockchar = "t"; var allowchar = "b"; var expression = '^' + blockchar + '[^\!\@\#\$\%\^\&\*\(\)\-\s\=\+\[\{\]\}\;\:\'\"\<\>\?\/\,\.\`\~\'' + allowchar + '][^\s]+$' var regexp = new regexp(expression); var elmval = document.getelementbyid("txt1").value; if (!elmval.match(regexp)) { return "validation failed"; }
can guide me on how incorporate 2 variables (blockchar
, allowchar
) in this? values in blockchar
should blocked , value in allowchar
should allowed.
you have different ways allow or forbid characters in string. lets see 3 basic examples:
example 1: give allowed characters in character class
/^[a-za-z0-9@]+$/
here, letters, numbers , arobase allowed, anchors @ begining , @ end ensure whole string must match pattern.
example 2: give unwanted characters in character class (using negated character class
[^.....]
)/^[^&#='")(]+$/
all characters allowed except &
#
=
'
"
example 3: check if string contain unwanted character
/[&#='")(]/
if result of match false , string length not null (or size want), string valid.
how allow new characters:
with example 1, it's easy, put in character class:
var charclass = 'a-za-z0-9@'; var allowchars = '{}'; var regexp = new regexp('^[' + charclass + allowchars + ']+$');
with example 2, more difficult because removing character class not easy. way use alternation:
var charclass = '[^&#=\'")(]'; var allowchars = '&#'; var regexp = new regexp('^(?:' + charclass + '|[' + allowchars + '])+$');
with example 3, same problem example 2, need remove characters class. can use negative lookahead instead before class:
var charclass = '[^&#=\'")(]'; var allowchars = '&#'; var regexp = new regexp('^(?![' + allowchars + '])' + charclass );
how forbid characters:
example 1: don't touch original class add negative lookahead (not followed by):
var charclass = '[a-za-z0-9@]+'; var blockchars = 'yz8'; var regexp = new regexp('^(?!.*[' + blockchars + '])' + charclass + '$');
characters y
z
8
in character class, negative lookahead make pattern fail if 1 these present in string.
example 2:
you can use same precedant trick, or can add forbidden characters (negated) character class:
var charclass = '^&#=\'")('; var blockchars = 'yz8'; var regexp = new regexp('^[' + charclass + blockchars + ']+$');
example 3: can add characters directly @ end of class too.
with these examples, can build code allow characters , forbid other characters dynamically:
example 1:
var charclass = 'a-za-z0-9@'; var allowchars = '{}'; var blockchars = 'yz8'; var regexp = new regexp('^(?!.*[' + blockchars + '])[' + charclass + allowchars + ']+$');
example 2:
var charclass = '^&#=\'")('; var allowchars = '&#'; var blockchars = 'yz8'; var regexp = new regexp('^(?:[' + charclass + blockchars + ']|[' + allowchars + '])+$');
example 3:
var charclass = '^&#=\'")('; var allowchars = '&#'; var blockchars = 'yz8'; var regexp = new regexp('^(?![' + allowchars + '])[' + charclass + blockchars + ']');
Comments
Post a Comment