This question already has answers here:
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
(10 answers)
Closed 5 years ago.
I know this has been asked a million times, but I just can't seem to crack it.
I have this:
function checkPassword(strPassword)
{
var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");
var blnResult = objPattern.test(strPassword);
return(blnResult)
}
...but it only seems to check the length, and not if there's a number?
What have I missed?
Edit:
The number can be anywhere in the string, not necessarily at the end.
Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable
If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$
You can just include both tests separately in your function:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
Demo:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
console.log(p+" ::: "+ checkPassword(p));
});
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I expect true return value that match condition below :
Only binary number (0 or 1)
Length not more than 8
I'm new to regex, i have googling and read JS RegExp from https://www.w3schools.com/Js/js_regexp.asp but still i don't get it.
I've tried
/[0-1]$/
but still didn't match the condition above
I expect boolean return from regex test if there is contain no other number except 0 or 1 and length not more than 8.
data: {
binRegExp: /[0-1]$/,
isBinary: false,
binNum: '', // get value from user input
},
computed: {
inputCheck(){
return this.isBinary = this.binRegExp.test(this.binNum)
}
}
code above is vue js
If there is solution, please answer below. Thank you
this will takes 1 to 8 [1-0]
const regex = /^[0-1]{1,8}$/
const text = "1010010"
console.log(regex.test(text));
Try
/^[01]{1,8}$/
let strings = ['11110001','1111000111','11112001'];
strings.forEach(x=> /^[01]{1,8}$/.test(x) ? console.log(x,'pass') : console.log(x,'not pass'))
This question already has answers here:
Regular expression to allow spaces between words
(13 answers)
Closed 3 years ago.
I have this phone regex, but I want it to accept spaces.
For example +57 52 5252255 should pass, but currently it's not passing.
also "+91 9 820 09 8200" should pass
so a space anywhere is acceptable
var phone_regex =/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
https://jsfiddle.net/ofn9knay/268/
In order to match that string, your second capturing group needs to accept 2 or 3 digits.
pattern = /^\s*(?:\+?(\d{1,3}))?[- (]*(\d{2,3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
test_strings = [
"+57 52 5252255",
"9820098200#301",
"+919820098200",
"+91-982-009-8200",
"+1 (866) 582-2655",
"+91 444 741 4000",
];
for (var i = 0; i < test_strings.length; i++) {
console.log(test_strings[i] + ": " + pattern.test(test_strings[i]));
}
We could try to tweak your existing complicated regex, but assuming you already have the phone number as an input in a variable, then one simple thing to do would be to just remove all whitespace:
var phone_regex =/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
var phone = "+91 9 820 09 8200";
phone = phone.replace(/ /g,'');
console.log(phone_regex.test(phone));
Then, just use your current regex as you were doing.
This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 5 years ago.
I have a number, for example:
25297710.1088
I need to add a bit between them and leave two characters after the point.
For example:
25 297 710.10
While I stopped at this:
$(td).text().reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse());
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
From this code I get the following:
25 297 710.1 088
Where $(td).text() I get a number from the cell of the row in the table.
If I have numbers, for example:
25297710.10
then i get:
25 297 710.10
It's ok.
What I need to do to leave two characters after the point?
You can use a RegExp to format the number/string. The input is converted to string using the relevant toString method.
function formatNumber(input) {
return input.toString().replace(/\d*(\d{2})(\d{3})(\d{3})\.(\d{2})\d*$/, "$1 $2 $3.$4");
}
var str = "25297710.1088";
var num1 = 25297710.1088;
var num2 = 2545454545454.2254;
var num3 = 232545454511112.3354122313123123;
console.log(formatNumber(str));
console.log(formatNumber(num1));
console.log(formatNumber(num2));
console.log(formatNumber(num3));
I think you can do next steps:
1) you have 25 297 710.10
2) you find position of dot symbol ->#pos
3) you replace bits in string in range between #pos and end of your string
4) you cut string after dot to 2 characters
This question already has an answer here:
Format a javascript number with a Metric Prefix like 1.5K, 1M, 1G, etc [duplicate]
(1 answer)
Closed 7 years ago.
To start with you need...
function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}
Then calling like so...
// m( ANY NUMBER HERE or VAR LIKE I USE,HOW DECIMAL PLACES)
m(110000,2)
However instead of the above's result of 0.11M, I would like it to display 110k.
What you have there is an example of an overly optimized script, lets make it more developer friendly an readable
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+n).length;
decimalPlaces=Math.pow(10,d); //raise 10 to the number of decimal places
var modLen = numberLength - numberLength%3;
var sufix = sufixes[modLen/3];
return Math.round(rawNumber*decimalPlaces/decimalPlaces(10,modLen))/decimalPlaces+ sufix;
}
Now it's easier to work with. We can see the issue is that we need to adjust for when the string is divisible by 3, so lets fix that.
function metricPrefix(rawNumber,decimalPlaces){
var sufixes= " kMFGPE";
var numberLength =(''+rawNumber).length;
decimalPlaces=Math.pow(10,decimalPlaces); //raise 10 to the number of decimal places
//THis is the change
//If the length is divisable by 3 take 3 off the length
var modLen = numberLength%3 == 0 ? numberLength - 3 - (numberLength%3) : numberLength - (numberLength%3);
console.log(modLen);
var sufix = sufixes[(modLen/3)]
console.log(sufix)
return Math.round(rawNumber*decimalPlaces/Math.pow(10,modLen))/decimalPlaces+ sufix;
}
$(document).ready(function(){
$("#result").html(metricPrefix(110000,2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
I can't figure out how to solve the following problem.
I have an array of numbers from 1 to 100.
I need to convert them to strings but to a length of 5.
So, for instance:
1 becomes 00001
2 becomes 00002
3 becomes 00003
4 becomes 00004
etc, etc..
It seems so easy but I cannot find a function. The best I found was .toFixed(n) which is the number of decimal points to use.
Here's a very simple padding function:
function padLeft(str, length, paddingCharacter) {
str = '' + str; //Make sure that we convert it to a string if it isn't
while (str.length < length) {
str = paddingCharacter + str; //Pad it
}
return str;
}
padLeft(123, 5, '0'); //00123