Validating the Time field in a Date Time field

Hi,

I have a question about how to validate against the time field in the Date Time field. I have used a simply java script condition to eliminate any alphabetical characters from the date field but when trying to accomplish this on the time field it is not possible.

The scenario comes in when a user are using the AM/PM function on the preferences set to yes. This enables the time to display the AM/PM abbreviation at the back of the time. The user is required to enter an exact time the case has been logged as this triggers the SLA component. The user might make a finger error and right the time out as 10:3AM where by CRM will default the value to 10:03 where +-27 minutes might be lost in the SLA time.

The code I use to validate the date field is below and this works perfectly but it doesn't validate the time field. I have tested it and if I would put any other character into the time field CRM automatically removes it with exception of "A" and "P". I have also set the users profile to not use the AM/PM function and display the half hour increments in the 24 hour version but the problem is still the same.

var Field = Values("case_timeofincident");
number = new String(Field);
arrayLength = number.length;

for (i=0; i<arrayLength; i++)

{
fld = new String(number.charAt(i));
if (fld==0 || fld==1 || fld==2 || fld==3 || fld==4 || fld==5 || fld==6 || fld==7 || fld==8 || fld==9 || fld==':' || fld=='/')
{

}
else
{
ErrorStr = "Only numeric characters can be used with exception of the :/ ";
Valid = false;

}

Regards

Rhijnhardt

  • 0

    Hiya,

    I don't want to sound like the beginning of a punchline, but I'd have figured that regular expressions would be good here. Here's one that will match a time field using AM/PM:

    /^(0?[1-9]|1[012])(:[0-5]\d)[APap][mM]$/

    Ideally, you might think of validating on the client side using onKeyUp or onBlur on that field, and then again when the data is submitted. On the client-side, you can use crm('case_timeofincident').val() and see if it returns a valid date string, at least if you're on 7.2 or 7.3.

    In the Validate script, you might look at using Moment.js, and try pulling in the user's preferred date format. You should be able to get at the date field by itself by using the FormValues collection. There's an example of using moment.js to parse the user's date format (on the client side) here:

    community.sagecrm.com/.../28731.aspx

    Hope this helps,

    Rob