2

I have simple use case where i want to validate date input it should be in the format "YYYY/MM/DD" if anything other format passed throw error so question how can i assign function to object property in typescript ?

main.ts

const ValidateRuless = [

    {element: "date", values: this.validateDate()}

];

function validateDate(date: string) {
    const date_regex =  /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/ ;
    if (!(date_regex.test(date))) {
        return false;
    }
}

1 Answer 1

4

To call a function, don't use this.functionName. Only use this. when calling methods of classes.

You probably also want to return a value in both cases in validateDate

Example:

const ValidateRuless = [

    {element: "date", values: validateDate("2018/05/17")}

];

function validateDate(date: string) {
    const date_regex =  /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/ ;
    if (!(date_regex.test(date))) {
        return false;
    } else {
        return true;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yyyy/mm/dd this is the correct format but its not taking it always return false
Debugging your regexp would be a different question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.