Home > OS >  Regex expression to refuse numeric sequences, such as "1234"
Regex expression to refuse numeric sequences, such as "1234"

Time:10-04

Using old webforms app...and...

Looking for help with a regex expression that won't allow numeric sequences such as '1234' but will allow 123 or 456 or 455 etc...just not 1234, 2345, 4567 etc...

So what I would like:

hello123 PASS
hello1234 FAIL
hello1244 PASS
hello1111 PASS
hello4567 FAIL
hello4566 PASS

So far I have got: .*\d{4}

which includes all CONSECTUIVE numbers, so anything with 4 numbers fails
dsfjs123 PASS
kdfjs23 PASS
fsjk1234 FAIL
1233sdflkds FAIL
23dklfjsk PASS
skfj24354lkj FAIL
dkfjsd23kjdlkj PASS

from: https://www.regextester.com/114733

Is this even possible what im asking for? i've been doing alot of research online and can't seem to find it?

Also...I would like to NOT ALLOW Alphabetic sequences, such as "abc" so:
ABC FAIL
ABB PASS
BBC PASS
BCD FAIL

any help or even a point in the right direction would be very much appreciated thank you

CodePudding user response:

You could use match() with the pattern 1234|2345|3456|4567|5678|6789|7890:

var inputs = ["hello123", "hello1234", "hello1244", "hello1111", "hello4567", "hello4566"];
inputs.forEach(i => console.log(i.match(/1234|2345|3456|4567|5678|6789|7890/) ? (i   " => FAIL") : (i   " => PASS")));

The regex alternation used above simply tries to find any of the 7 disallowed 4 length numerical sequences.

  • Related