Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i have a number limit i.e. 000 now i want to increment it by 1 so the next limit will be '001' but when i try to add it gives me '1',
also if somehow i figure out to maintain it '001', the second problem is after '009' there should be '010'. Advance Thanks for you efforts.
Thats padding.
function pad(num, size) {
var s = "000" + num;
return s.substr(s.length-size);
}
Assuming you'll never need more than 3 digits
[Edit] - For those who like early technology i.e ES7
There is a new method called padStart()
padStart - The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start (left) of the current string.
Example 1
const digits = 3
digits.toString().padStart(3, '0') // prints 003
Example 2
const digits = 76
digits.toString().padStart(3, '0') // prints 076
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to check if a number has a 2 decimal place precision. I know how to convert a number to a 2 decimal place precision like this:
var myNumber = 2.456;
var currentNumber = parseFloat(myNumber);
currentNumber = currentNumber.toFixed(2);
console.log(currentNumber);
so If I enter:
1 this will turn into 1.00
2.457 this will turn into 2.46
but how can I check if the result of my code has a 2 decimal place precision? Thanks a lot in advance!
Parse the number into a string, split by a period and check whether the second item's length is 2:
function isPrecise(num){
return String(num).split(".")[1]?.length == 2;
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise(1))
console.log(isPrecise("1.23")) //works if its a string too
You can also use a regex:
function isPrecise(num){
return /\d+\.\d{2}/gm.test(String(num));
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise("1.23")) //works if its a string too
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to create a unique id which I can assign to a user
it needs to be between 6 and 9 characters
it can be alphanumeric
I have seen this answer
Random alpha-numeric string in JavaScript?
but it generates long ID, I want the ID to be between 6 and 9 characters
How can achieve this in javascript?
//Generating random
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//Generating unique id
function id(n){
var char = "";
for(let i = 0;i<n;i+=1){
char += String.fromCharCode(getRandomInt(32, 100));//alphanumeric chars
}
return char;
}
//test
for(let i=0;i<10;i+=1){
var uniqueId = id(getRandomInt(6,9));//between 6 and 9 characters
//result
console.log(uniqueId, uniqueId.length);
}
What you could do is to define an array of all allowed characters. Afterwards you loop over it as often your ID needs to be long. (Already posted by #Thomas Juranek)
Main issue here: how can be confirmed that it's unique? What is memorable? Do you have a storage were all used IDs are in (database, file, etc.)?
Another approach would be much more complicated: Combine parts of (fe. your MAC address) and a current timestamp. You can hash it to disguise the reference of this data.
But it mainly depends on your use case. Maybe this one gives you also a spin
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Say I have a String
Thi__(1)__s i__(2)__s a test__(5)__ mess__(4)__age __(6)__
I want to replace the numbers in between the pattern __()__ by that number-1, that is, 2 with 1, 3 with 2 and so on. And there is a condition, that number should be greater than 3.
So my final string will look like
Thi__(1)__s i__(2)__s a test__(4)__ mess__(3)__age __(5)__
I know how to make a logic for that but as I am new to Javascript/Jquery I am looking for a better way.
Any help would be appreciated.
var str = "Thi__(1)__s i__(2)__s a test__(5)__ mess__(4)__age __(6)__";
var replaced = str.replace(/(__\()(\d+)(\)__)/g, function(_, left, val, right){ //replace all (digits)
val = +val; //convert to number
return left + (val > 3 ? --val: val) + right; //use whatever convert logic you need
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my regular expression
/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/
There will be one condition: the length of every string after first 40 characters must be 16 characters. This 16 characters string will be non repeating and minimum of 2 times and maximum of 10 times. So i want to get the length of this sub-string which should be 16.
Here is input string:
string input="PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522"
I want to match the length of "GEX2421262506027" and "GEX2437345435522". Like this, there will be 10 strings max. I want to validate if this length should be 16 characters.
try this
var pattern = /^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}
if you just want the last 16 characters:
var string = 'PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522';
var matching = string.match(/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/);
console.log(matching[1]);
console.log(matching[1].length);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my goal numbers from the three digits to 9 digits. for example
Valid options
175
1.250
14.365
145.985
1.562.745
17.487.984
999.999.999
Now this is the regular expression that i develop
/^\d{1,3}\.\d{1,3}\.\d{1,3}$/
My problem it's that this is accepting this values
176.57.117 <---- this is not valid value
176.257.7 <---- this is not valid value
176.257.17 <---- this is not valid value
Thanks for your help
UPDATE
I'm trying to make a regular expression that validates positive natural numbers from three digits to 9 digits and separates the thousand unit and the million unit with a point
/^\d{1,3}(\.\d{3}(\.\d{3})?)?$/
What you really want is 1 to 3 digits possibly followed by 1 or 2 additional sets of three digits. Your original reg-ex just said "3 sets of 1-3 digits" which isn't really what you want. It also would have failed to accept your first several valid examples since they had less than three sets of digits.
Just split the string for . and then check string length ... in each of array indexes (i think it will be more self explaining than regexp)