Reputation: 313
Hello every body i need help with moment js i've an input field called Start and another called stop
start = moment().format('LT'); // works when i click on the play button
stop = moment().format('LT'); // works when i focus on the counter input beside the play button --> for test
i want to change the start input field manually so i want a validation function that takes the input value and check if the input is valid or not on this form LT
for ex: when i delete the value of the input which is 6:39 PM
as below in the picture and type for example 6:02:00 PM or 1:00 PM or
add a string 5:dfds2 PM
i want to console log any error message and return the previous value to the input again
also when i remove the current value and add a number like '1 for example' without am or pm
so it determines if the number is before or after than stop input value and type in the input field like this 1:00 AM or 1:00PM
i used this function to validate the start input field but it gives me wrong answers
function validate(inputVal) {
let temp =this.startTime;
let x = temp;
if(moment(inputVal, "hh:mm").isValid()) {
x= moment(inputVal, "HH:mm").format('hh:mm A');
console.log("inputVal is: " + inputVal + " and x is: " + x);
this.startTime = x
} else {
this.startTime = "temp";
console.log("no");
}
}
here is the pic for more info u can visit toggl website my idea is taken from there any help ?! thanx in advance
Upvotes: 1
Views: 110
Reputation: 3861
I went ahead and cleaned up your function a bit, reduced the logic, now you should just make sure the moment format is what your looking for
function validate(val) {
let parsedTime = moment(val, "hh:mm");
if (parsedTime.isValid()) {
this.startTime = parsedTime.format('hh:mm A');
}
}
Upvotes: 1