I'm trying to validate a username in jquery using a regex, with following rules
String must start with a letter
String can have 0(m) to 4(n) digits anywhere or no more than 4(n)
Size of the string must be between 6(p) and 20(q)
Following should fail
1djgWWq
(starts with a digit)wwwhg
(length less than 6)ky12yu781
(more than 4 digits)
Following should pass
tableten
table10
tab7le10
I tried following regex which is good, except its not match 4 digits anywhere in the string
^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$
Sample regex101
Note: m,n, p,q integer variable
EDIT 1
Sebastian Proske> Yes letters numbers and _ - I want to try validate fields in a form using different regex for each field, so regex per one filed, I want to stick to one regex for cleaner code. bellow is sample untested code to give you a idea what I'm trying to do. Sebastian Proske answer is the best one for my work. Thanks everyone.
Sample 1<input id="ds1" type="text" class="validate" data-validate='{"regex":"^[a-z0-9]{4,7}$"}' /><br/>
Sample 2...
Sample 3...
Active Sample <input id="as" type="text" class="validate" data-validate='{"regex":"^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$"}' /><br/>
<input id="f1SubmitBtn" type="button" value="Test" onclick="f1Submit();" /><br/>
<script>
function f1Submit() {
$('.validate').each(function() {
if (!$(this).data('validate').regex.test(this.value)) {
console.log(this.value + " No!");
}
});
</script>
I'm trying to validate a username in jquery using a regex, with following rules
String must start with a letter
String can have 0(m) to 4(n) digits anywhere or no more than 4(n)
Size of the string must be between 6(p) and 20(q)
Following should fail
1djgWWq
(starts with a digit)wwwhg
(length less than 6)ky12yu781
(more than 4 digits)
Following should pass
tableten
table10
tab7le10
I tried following regex which is good, except its not match 4 digits anywhere in the string
^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$
Sample regex101
Note: m,n, p,q integer variable
EDIT 1
Sebastian Proske> Yes letters numbers and _ - I want to try validate fields in a form using different regex for each field, so regex per one filed, I want to stick to one regex for cleaner code. bellow is sample untested code to give you a idea what I'm trying to do. Sebastian Proske answer is the best one for my work. Thanks everyone.
Sample 1<input id="ds1" type="text" class="validate" data-validate='{"regex":"^[a-z0-9]{4,7}$"}' /><br/>
Sample 2...
Sample 3...
Active Sample <input id="as" type="text" class="validate" data-validate='{"regex":"^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$"}' /><br/>
<input id="f1SubmitBtn" type="button" value="Test" onclick="f1Submit();" /><br/>
<script>
function f1Submit() {
$('.validate').each(function() {
if (!$(this).data('validate').regex.test(this.value)) {
console.log(this.value + " No!");
}
});
</script>
Share
Improve this question
edited Sep 2, 2016 at 1:20
Udaan
asked Aug 31, 2016 at 5:51
UdaanUdaan
871 gold badge2 silver badges14 bronze badges
4
- Is there any reason why you cannot use multiple statements to do what you're doing to check and what not? Or must everything fit into one regex? – A. L Commented Aug 31, 2016 at 5:58
- If you use one statement to check for more than for digits such as var regex = /\d+/g; and then do this regex.exec(string). This will return an array with all digits and you can check for the length. – Diego Gallegos Commented Aug 31, 2016 at 6:00
- simple-regex. – SilentTremor Commented Aug 31, 2016 at 6:01
- I want to try validate fields in a form using different regex for the field, so per one filed, I want to stick to one regex for cleaner code.. – Udaan Commented Sep 1, 2016 at 23:56
3 Answers
Reset to default 3From your original regex I assume that _
and -
are also allowed characters. You can test your names with the regex /^(?!(?:\D*\d){5})[a-z][\w-]{5,19}$/i
.
A little breakdown:
^
is an anchor for the start of the string(?!(?:\D*\d){5})
is a negative lookahead checking for the presence of 5 digits (which will cause the match to fail)[a-z]
the first character has to be a letter[\w-]{5,19}
length between 6 and 20, word characters ([a-zA-Z0-9_]
) and minus are allowed$
is an anchor for the end of the stringi
is the case-insensitivity modifier
See Regex101
I could acplish this (I hope) with two regexes, and doing and
between the results,
The first one is
^[a-zA-A][a-zA-Z0-9]{5,19}$
which makes the string start with a letter and limits the length to 6-20
and the second one is
^[^\d]*?(\d|\d[^\d]+){0,4}$
which limits the number of digits to 0-4
I believe you want regex for speed, and I think doing logical and
between 2 regex results can also be acceptable.
You have to add some logic to achieve the behaviour you want:
<script>
function matches(str, m, n, p, q) {
var numDigits = str.replace(/[^0-9]/g, '').length;
if(m <= numDigits && numDigits <= n) {
return new RegExp('^[A-Za-z].{' + (p - 1) + ',' + (q - 1) + '}$').test(str);
} else {
return false;
}
}
var tests = ['1djgWWq', 'wwwhg', 'ky12yu781', 'tableten', 'table10','tableten'];
for(var i = 0; i < tests.length; i++) {
console.log(tests[i] + ' -> ' + matches(tests[i], 0, 4, 6, 20));
}
</script>
The results are:
1djgWWq -> false
wwwhg -> false
ky12yu781 -> false
tableten -> true
table10 -> true
tableten -> true
Here the jsfiddle: https://jsfiddle/z7a6fgfg/