7 or 10 digits input validation using javascript regular expression? [duplicate] - javascript

This question already has answers here:
Is there a regex quantifier that says "either x or y repeats"?
(4 answers)
Closed 5 years ago.
I want user to type either 7 digits number or 10 digits number in a textbox.
My code:
var numberfilter = /^([0-9]{7})|([0-9]{10})$/;
var x=document.forms["myForm"]["number_field"].value;
if(numberfilter.test(x)==true)
alert("Valid");
else if(numberfilter.test(x)==false)
alert("Invalid");
The above regular expression is showing "valid" for 7 or more digits also. Please help!

The | operator applies to the whole expressions on the left and right side not just the group in brackets, unless you put it inside a group. So your expression is basically:
^([0-9]{7}) or ([0-9]{10})$
what you need is duplicate the ^$ anchors on both sides of |:
^([0-9]{7})$|(^[0-9]{10})$
or
group the whole thing apart from anchors:
^(([0-9]{7})|([0-9]{10}))$
EDIT: The above explains where you went wrong, but the solution is not the best one. See the reference in comment for the slicker solution:
^(\d{7})(\d{3})?$

try regex
/^([0-9]{7})$|^([0-9]{10})$/ (whole word length is 7 or whole word length is 10)

Your regex actually says "Match if the first 7 characters are numbers OR if the last 10 characters are numbers".
This is because you forgot to place the anchors on both ends of the conditionals. Try something like this.
^([0-9]{7})$|^([0-9]{10})$
You can see it work here.

DEMO
var numberfilter = /^\d{7}(?:\d{3})?$/;
var x='6666666666';
var y='66666666';
var z='6666666';
test(x);
test(y);
test(z);
function test(x){
if(numberfilter.test(x)==true)
console.log(x+" Valid");
else if(numberfilter.test(x)==false)
console.log(x+" Invalid");
}
Here is Regex /^\d{7}(?:\d{3})?$/ checks for first 7 digit with (?: capturing group for next 3 digit

Related

Regex to not match a pattern in Javascript [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 7 months ago.
I need a reqular expression to not match this (/^[a-zA-Z][a-zA-Z0-9]+$/) pattern, where the string needs to start with alphabet followed by number and alphabet, with no special characters.
I tried with (/^?![a-zA-Z]?![a-zA-Z0-9]+$/) and not able to get appropriate answer.
Example:
P123454(Invalid)
PP1234(Invalid)
1245P(valid)
##$124(valid)
Thanks in advance.
^ means start with, So it should start with an alphabetic letter, then any number \d of alphabetic letters a-z with i case insensitive flag.
const check = (str) => {
return /^[^a-z].*/i.test(str)
}
console.log(check('P123454'))
console.log(check('PP1234'))
console.log(check('1245P'))
console.log(check('##$124'))
This regex might be helpful:
/^[^a-zA-Z]+.*$/g
Your every valid input (from the question) should be a match.
Regex 101 Demo
Explanation:
Does not allow string starting with a-zA-Z
Everything after than is allowed (I'm not sure if this is a requirement)

Why quantifier doesn't work [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 6 years ago.
I have the following regex:
^(?:[\w]+?\/)?[\w]+?#[\w]+?\.[\w]+?$
Now I need to limit the overall string length by 25 characters:
I tried the following:
^((?:[\w]+?\/)?[\w]+?#[\w]+?\.[\w]+?){0,25}$
But it still matches regexes over 25 characters, why?
A quantifier is applied to the token preceding it. In your case, that's an entire group, and that group can match much more than a single character.
Do the length check separately, using a positive lookahead assertion:
^(?=.{0,25}$)(?:\w+\/)?\w+#\w+\.\w+$
As you can see, your regex can also be simplified quite a bit (no need for lazy quantifiers and character classes).
why not simply
if ( inputStr.length <= 25 && /^(?:[\w]+?\/)?[\w]+?#[\w]+?\.[\w]+?$/.test( inputStr ) )
{
//your logic
}
If we call your original regex A
^(?:[\w]+?\/)?[\w]+?#[\w]+?\.[\w]+?$
Then your next regex can be expressed more simply as
^(A){0,25}$
So you're not matching only 25 characters, you're matching A 0 to 25 times

Regular Expression Maximum Number Of Characters Javascript [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 6 years ago.
So i have this regex:
var regex = /^[\S]+(\-[\S]+)$/
i want to add one more rule, the rule is that maximum number of characters is 6 or something, how do i do it? my full code looks like this
var regex = /^[\S]+(\-[\S]+)$/
var word = "a-a";
if(word.match(regex)){
console.log("matched");
}else{
console.log("did not match");
}
console.log(word.length);
i've tried var regex = /^([\S]+(\-[\S]+)){6}$/ but this means they must repeat a-a 6 times, i want the maximum number of characters, something like this:
var word = "123-56"
to be matched, how do i do it?
and i don't want to use .length because i want to implement it into regex
You can use a lookahead:
var regex = /^(?=.{0,6}$)[\S]+\-[\S]+$/

regular expression for password [duplicate]

This question already has answers here:
Regular Expression for password validation
(6 answers)
Closed 9 years ago.
I need help coming up with a regular expression for javascript which has to verify a password.
The password (8 characters long, at least one non-letter character)
it would also be useful to make sure the user does not have spaces at the beginning or end
This is a pattern that will match what you asked for
pattern = /^(?=.*[^a-z])\S.{6}\S$/i;
Depends how 'complicated' you want to get but I find either if the following useful:
/*
No more than 4 same characters
- One digit
- One uppercase
- One lowercse
- One 'punctuation' mark
- between 8 and 20 characters
*/
re = /^(?!.*(.)\1{4})((?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[^\w\d\s])).{8,20}$/;
/*
- One Digit
- One lower case
- One upper case
- Maximum 2 repeating char
- between 6 and 20 characters
*/
re = /^(?!.*(.)\1{2})((?=.*[a-z])(?=.*[A-Z])(?=.*\d)).{6,20}$/;
These are not my regexp's as I found them on the web but can't remember where.

JavaScript: \\d{4} RegExp allows more than 4 digits [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
Have following validation for year value from text input:
if (!year.match(new RegExp('\\d{4}'))){
...
}
RegExp equals null if numeric of digits from 0 to 3. It's OK.
In case 4 digits it returns value.It's OK.
In case more than 4 digits it returns value again,that it's NOT OK.
Documentation says {n} declaration means exact number,but works like:
exact+
With such ugly validation it work's fine:
if (!year.match(new RegExp('\\d{4}')) || year.length>4){
...
}
I wish to utilize RegExp object only.
Yes it would allow more than 4 digits since it would be a partial match use the ^ and $ to mark the beginning and the end of the string.
if (!year.match(new RegExp('^\\d{4}$'))){
...
}
If you include ^ in your regex it matches the beginning of the string, while $ matches the end, so all up:
^\d{4}$
Will match only against beginning-of-string plus four digits plus end-of-string.
Note that regex literal syntax is generally a bit simpler than saying new Regex():
/^\d{4}$/
// is the equivalent of
new RegExp('^\\d{4}$')
Note that in the literal syntax you don't have to escape backslashes like with the string you pass to the new RegExp(). The forward slashes are not part of the expression itself, you can think of them like quotation marks for regexes.
Also, if you just want to check if a string matches a pattern (yes or no) without extracting what actually matched you should use the .test() method as follows:
if (!/^\d{4}$/.test(year)) {
...
}
It's matching the first four digits and then the fact that there's any remaining digits it neither here nor there. You need to change your regex so it stops after these four digits, say, by using the string termination anchors:
^\d{4}$
Try instead:
'^\\d{4}$'
What you had will match anything with 4 digits anywhere, such as asd1234asd or 123456789

Categories

Resources