Regular expression between two numeric values [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to match a number which is less than or equal to 100?
i need a regular expression between these two values 1000 <= x <= 500000 im trying with this one that i constructed but doesnt seem to work
/(1[8-9]|[8-9]|[8-9]|5[0-9]|[0-9]|[0-9]|[0-9]|[0-9])/
Any ideas? thanks in advance!

Is there a particular reason you don't just test the numbers as numbers?
var yourNum = parseInt(yourString, 10); // use parseFloat if it has decimals
if (yourNum >= 1000 && yourNum <= 500000) {
// success
} else
// fail
}

\b([1-9][0-9]{3,4}|[1-4][0-9]{5}|500000)\b

Match the cases 1000-9999, 10000-99999, 100000-499999 or 500000:
([1-9]\d{3}|[1-9]\d{4}|[1-4]\d{5}|500000)
Or combining the two first:
([1-9]\d{3,4}|[1-4]\d{5}|500000)

Related

How do I get the amount of numbers in a string? [duplicate]

This question already has answers here:
Count the number of integers in a string
(6 answers)
Closed 4 years ago.
I currently have a text input in my HTML document and some buttons below it. When pressed, these will run a function input(some number). I have added a feature to the function that prevents the length of the string for the input from being above 5. However, sometimes the string (currentAnswer) will be set to something like -768.. When this happens, the function will not let you enter any more numbers, when instead you should be able to enter 2 more digits. So how would I find the length of just the numbers in a string? Here is my code:
function input(num) {
currentAnswer = document.getElementById("answerBox").value;
answerLength = currentAnswer.length;
if (answerLength < 5) {
document.getElementById("answerBox").value = currentAnswer + num;
answerLength = answerLength + 1;
}}
As you can see, I am using currentAnswer.length to get the length of the string. But I only want the amount of digits. Can someone please help me?
Count the number of integers in a string
alert("g66ghy7".replace(/[^0-9]/g,"").length);
:)

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);

toString(16) with leading zeroes [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
When you convert a small number to a hexadecimal representation, you need leading zeroes, because toString(16) will return f for 15, instead of 00000f. Usually I will use the loop like this:
var s = X.toString(16); while (s.length < 6) s = '0' + s
is there a better way in JavaScript?
UPD: The answer suggested answer How can I create a Zerofilled value using JavaScript? is not what I am looking for, I look for a very short code, that is suited specifically for 24 bit integers converted to a hexadecimal looking strings.
How about
('00000'+(15).toString(16)).substr(-5)
Maybe this:
var s = X.toString(16);
s = "000000".substr(0, 6 - s.length) + s;

Javascript Regex for decimals [duplicate]

This question already has answers here:
Simple regular expression for a decimal with a precision of 2
(17 answers)
Closed 9 years ago.
In Javascript, I am trying to validate a user input to be only valid decimals
I have the following JSFiddle that shows the regex I currently have
http://jsfiddle.net/FCHwx/
var regex = /^[0-9]+$/i;
var key = '500.00';
if (key.match(regex) != null) {
alert('legal');
}
else {
alert('illegal');
}
This works fine for integers. I need to also allow decimal numbers (i.e. up to 2 decimal places)
I have tried many of the regex's that can be found on stackoverflow
e.g.
Simple regular expression for a decimal with a precision of 2
but none of them work for this use case
What am I doing wrong?
This should be work
var regex = /^\d+(\.\d{1,2})?$/i;
Have you tried this?
var regex = /^[0-9]+\.[0-9]{0,2}$/i;
I recommend you to not use REGEX for this, but use a simple !isNaN:
console.log(!isNaN('20.13')); // true
console.log(!isNaN('20')); // true
console.log(!isNaN('20kb')); // false
Try this:
\d+(\.\d{1,2})?
d is for digit
d{1,2} is for 1 digit before . and at least 2 digits such as 0.51

javascript parseInt [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parseInt a string with leading 0
If I parseInt("01") in javascript its not the same as parseInt("1")???
start = getStartEugene("MN01");
start2 = getStartEugene("MN1");
getStartEugene: function(spot) //ex: GT01 GT1
{
var yard = spot.match(/[0-9]+/);
var yardCheck = parseInt(yard);
if (yardCheck < 10)
return "this"+yard;
else
return "this0"+yard
}
I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09
But i am not getting it. Anyone know why?
You need to add the radix (2nd) argument to specify you are using a base 10 number system...
parseInt("01", 10); // 1
This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as #jondavidjohn correctly pointed).
parseInt("10"); // returns 10
parseInt("010"); // returns 8

Categories