Regular Expression Maximum Number Of Characters Javascript [duplicate] - javascript

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]+$/

Related

Regular Expression to check Special characters except hypen and forwardslash [duplicate]

This question already has answers here:
Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
(7 answers)
Closed 3 years ago.
I would like to know regex code to check whether it contains Special characters other than hypen and forwardslash in javascript
function containsSpecialCharacters(str){
var regex = /[~`!#$%\^&*+=\-\[\]\';,/{}|\\":<>\?]/g;
return regex.test(str);
}
var result = containsSpecialCharacters("sample"); // false
var result = containsSpecialCharacters("sample-test"); // false
var result = containsSpecialCharacters("sample++"); // true
var result = containsSpecialCharacters("/sample/test"); // false
You can use a normal regex to search for all ASCII special characters, like this one, and then just remove the hypen and backslash:
/[!$%^&*()_+|~=`{}\[\]:";'<>?,.]/
If you want to avoid any other special character, you just have to remove it from the regex.
You can test this regex here: https://regex101.com/r/gxYiGp/2
If you need to support only English characters, it would be far easier to list the exceptions than those you wish to match:
/[^\w\s\\\-]/.test(myString);
As noted, though, this will fail if you need to support international characters.
you can just negate it so it looks shorter, like this :
[^\s\w-]

Check if first character of string is digit or whitespace with regex or alternative [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check if a given string's first character is a word character/not a digit or white-space and return a boolean.
I'm trying to use regex for this. What I have is:
function isFirstIndexCharacter(str){
var regex = RegExp('w')
return regex.test(str[0]);
}
I always get false no matter what my input is. Thanks for any help on this.
You can do something like this -
function isFirstIndexCharacter(str){
return /^\w/.test(str[0]);
}

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

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

While checking whether a string minimum 5 digits, no alpha nor special characters gives unexpected results [duplicate]

This question already has answers here:
Regular expression for number with length of 4, 5 or 6
(5 answers)
Closed 7 years ago.
I'm trying to check whether a string contains a minimum of 5 digits, no alphabets or special characters.
My code is as follows:
var re = /^[a-zA-Z!##\$%\^\&*\)\(+=._-]{5,}$/g;
But when I tried with prm= "5653636*" or "32266" or "256"
it always returns false
when I do the following:
re.test(prm)
To see if a string doesn't have an alphabet [^a-zA-Z];
^ -means NOT and [] is used to match range of characters
and u can use length property to find the length.
The following snippet will match is a string doesn't have an alphabet and it has its length is greater than 5.
var _string = "1234567";
var re = /[^a-z]+/i;
if(re.text(_string)){ // true
}
if(_string.length > 5){ //true
}
Given that I got your requirements right, I think this is what you're looking for:
[^a-zA-Z]{5,}
At least 5 characters
No alphabetical characters (A-Z)
Numbers, special characters and white space are allowed
If you want to allow the special characters from your regex only, you can do:
[0-9!#\#\$%\^&\*\)\(\+=\._-]{5,}

Javascript Regular Expression to match six-digit number [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?
You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/
Insert an extra "\" in your regexp.
You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Categories

Resources