MoneyMgr/moneymgr_web/src/utils/FormUtils.tsx
2025-04-03 22:37:22 +02:00

33 lines
767 B
TypeScript

import { LenConstraint } from "../api/ServerApi";
/**
* Check if a constraint was respected or not
*
* @returns An error message appropriate for the constraint
* violation, if any, or undefined otherwise
*/
export function checkConstraint(
constraint: LenConstraint,
value: string | undefined
): string | undefined {
value = value ?? "";
if (value.length < constraint.min)
return `Please specify at least ${constraint.min} characters !`;
if (value.length > constraint.max)
return `Please specify at least ${constraint.min} characters !`;
return undefined;
}
/**
* Check out whether a given URL is valid or not
*/
export function checkURL(s: string): boolean {
try {
return Boolean(new URL(s));
} catch {
return false;
}
}