# Function not working for onValidate

**URL:** <https://www.five.org/t/function-not-working-for-onvalidate/231>\
**Category:** Support\
**Created:** [June 7, 2023, 5:51pm UTC](https://www.five.org/t/function-not-working-for-onvalidate/231 "2023-06-07T17:51:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shweta](https://yyz2.discourse-cdn.com/flex030/user_avatar/www.five.org/shweta/32/186_2.png) [@shweta](https://www.five.org/u/shweta)\
**Post date:** [June 7, 2023, 5:51pm UTC](https://www.five.org/t/function-not-working-for-onvalidate/231/1 "2023-06-07T17:51:32Z")

</div>

I want that person should be at least 5 days old, so I created a function for that and added it to onValidate event. But I am not getting any error if I entered wrong date.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/five/original/1X/99493c3a683a2968f6fce134737388e6d152a8b7.png)

This is the function I wrote

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/five/original/1X/784124dd8bcec7993827585b1997fe4fd7a08ab7.png)

This is the form field.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/five/original/1X/97149187c412487f075d941d7304a30589b6a197.png)

This is function added to event.

I have tried using two code  
1.

```plaintext
    const date = new Date(five.field.DateOfBirth);
    let diffInMilliseconds = new Date() - date;

    let diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);

    if(diffInDays < 5){
        return five.createError(result, 'Needs to be atleast 5 days old');
    }
    
    return five.sucess(result);
}

```

1. 

```auto
function CheckBirthDate(sender, context, result) {
    const date = new Date(sender.value);
    let diffInMilliseconds = new Date() - date;

    let diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);

    if(diffInDays < 5){
        return five.createError(result, 'Needs to be atleast 5 days old');
    }
    
    return five.sucess(result);
}

```

Both did not work. What am I doing wrong? Can someone help, please

Thanks!

---

<div class="post-metadata">

**Author:** ![Pranoy](https://yyz2.discourse-cdn.com/flex030/user_avatar/www.five.org/pranoy/32/281_2.png) [@Pranoy](https://www.five.org/u/Pranoy)\
**Post date:** [June 7, 2023, 10:49pm UTC](https://www.five.org/t/function-not-working-for-onvalidate/231/2 "2023-06-07T22:49:57Z")

</div>

Hi Shweta,

Try moving the function from the `onValidate` event to the `onComplete` event of the form and see if it works correctly,

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/five/original/1X/55069a192d14fff9fa5e9d9a1f93fd7683624b95.png)

also, I have noticed a typographical error in your code.  
instead of:

> return five.sucess(result);

It should be:

> return five.success(result);

Let me know if this works

---

<div class="post-metadata">

**Author:** ![shweta](https://yyz2.discourse-cdn.com/flex030/user_avatar/www.five.org/shweta/32/186_2.png) [@shweta](https://www.five.org/u/shweta)\
**Post date:** [June 8, 2023, 5:00am UTC](https://www.five.org/t/function-not-working-for-onvalidate/231/3 "2023-06-08T05:00:10Z")

</div>

Hello,

This worked. Thanks a lot!
