comparing dates in Javascript - Results not consistent [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to check 2 date fields and compare to see which date is ahead, behind or the same
I am trying to implement a validation which should compare two dates and give an alert message.
entrydate is a text feild in our ASP page and so is vdata. I should check and make sure that vdata is always greater than or equal to entrydate. The code below is not working.
Please help to identify what the problem is with the this code:
if(document.Step2.entrydate.value <= document.all(vData).value)

The problem is that the text in an input box is just that, text. You are trying to compare dates, so you will need to convert those strings into dates and compare the dates.

The problem is that the value of a textfield is a string. So you're basically comparing strings, and not dates.
You would first need to parse your string into an actual Date object before you can do a reliable comparison. How you would do that depends on the format of the data

Related

How do you exclude only some numbers from string input on Javascript? [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed last month.
So I'm wanting to figure out how do you exclude on some numbers from a string input. For example, If I were to input this "https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494", I'd want "?variant=41679925772494" remove and keep the rest the same. I can't use a constant of "?variant=41679925772494" because I'm wanting it work across all string inputs, not the same one. what I have is below, but it removes all numbers within the link, rather than whats after "?variant=". Does anyone have any ideas on how I can go about this?
let updatedLink = link.replace("?variant=", "")
let variantLink = updatedLink.replace(/[0-9]/g, "")```
If you want to parse URL's, using regex is not really the best option.
Browsers have the URL class that does exactly what your after.
eg.
var n = new URL('https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494');
n.searchParams.delete('variant');
console.log(n.toString());

Regex for finding all the numbers between any two numeric values (range) in JavaScript [duplicate]

This question already has answers here:
How to match numbers between X and Y with regexp?
(7 answers)
Closed 7 years ago.
First of all, i know Regular expressions isn't the best tool to achieve what I want here. I have done enough research to know that bit. Still, The problem I am stuck in requires me to make up a regex to find the values between some lower and upper bound values.
So here is the problem, I have a large set of data, let's say ranging between 1 and 1000000. That data is not under my direct control, I cannot manipulate the data directly. Only way of finding out (searching) some values from that data is regex.. Now, the user can give two values, a minimum value and a maximum value and I need to construct a regex based on these two values and then query the large data set using the regex to get all the values lying between the set range. So, if my data contains [1,5,7,9,15,30,45,87] and user sets the range min:10, max:40. The regex should filter out values 15, 30.
From whatever I have searched, I know it is very much possible to build a regex for finding out values between fixed values (if we know them beforehand) for example, values between 1 to 100 can be found by:
^(100|[1-9][0-9]?)$
But what gets so tricky about my problem is that the input range can be anything from pretty much 1 digit values to up to 10 digit values. 10000-550000 can be an example user input for a large data set.
I know this will require some complex logic and loops involved on the basis of number of digits in the lower bound and number of digits in the upper bound of the range and then some recursive or other magical logic to build a regex that covers all the number lying in that range.
I've been filling up pages to come up with a logic but I'm afraid it surpasses my knowledge of regex. If anyone has ever done something like this before or try to point me in the right direction or attempt it him/herself - it'll be quite helpful. Thanks.
The language I will be using this in is JavaScript and I read somewhere that JS doesn't support conditional regex, keeping that in mind, solution doesn't have to be in specific to a language.
If your task is to get numbers between min and max value from the dataset, you can try filter method.
Var filteredResults = Dataset.filter(function(item){
If(item < max && item > min)
Return item
}
)

Mask a input in HTML using javascript [duplicate]

This question already has answers here:
Mask US phone number string with JavaScript
(9 answers)
Closed 7 years ago.
How can I mask a HTML input text with a mask for phone number?
Here in Brazil, in some states, the number format can change.
For example:
(dd)xxxx-xxxx
this is the default number format, where dd is the state code, and the x's are the numbers.
BUT, in some states, the number nine is added to the phone number, like this:
(dd)9xxxx-xxxx. (let's call it "new format")
the var dd isn't important, what I want to do is when the user type the new format, the hyphen automatically appears after the 4th X.
When the users start entering the phone number, you could have a regex such as ^(\d{2})9 and regex match the string to see if the hyphen needs to be added at a different place.
You could use something like formatter.js to do the job for you!
I have not tried it, but the demos look quite solid.
Try using two inputs with different masking (8 digit & 9 digit respectively) and switch them based on the state selected in the form.

javascript now to create two ways currency format? [duplicate]

This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 8 years ago.
I have found this link have a simple and useable funciton to convert number to currency.
www.mredkj.com/javascript/nfbasic.html
but how to turn it back form currency to number...
I already try this :
[http://jsfiddle.net/vb2cb1tm/3/]...
But its failed...
I try to read a formatted string using javascript and make a counting on this valua...
I think something as simple as
function numbers(nStr){
return Number(nStr.replace(/[\$\,]/g,''));
}
might do the trick, though it's hard to tell without seeing your original code.
function cur2num(cur) {
return parseFloat(cur.replace(/,/g,"");
}
if currency signs try /[^0-9]\.\-]/g as first argument to the replace

Replace 56666.666666666664% percentage in to three digit percentage using javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 9 years ago.
I have calculated some data usign javascript and result is printing like this (56666.666666666664%). and I need to convert the result in like 56.66%. Below is a Javascript which print the above result.
$scope.totaTaken = (totalT / data.length)*100;
If you're looking for a JavaScript solution (where you do it in a controller or directive), the given comments answer your question (see toFixed). If you're looking for an AngularJS solution, where you format the value in the view, check out the number filter which was made for this!

Categories

Resources