Add IBAN formatting for improved display readability

Implemented IBAN formatting to display with proper spacing:
- Added formatIban() utility function in formatStrings.ts
- Format: Groups of 4 characters separated by spaces (e.g., HR12 3456 7890 1234 5678 9)
- Applied formatting to IBAN field display in AccountForm
- Updated validation to check cleaned IBAN (without spaces)
- Maintains backward compatibility with server-side validation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2025-11-17 20:40:10 +01:00
parent 6d6c65d4e3
commit ba77169554
2 changed files with 30 additions and 5 deletions

View File

@@ -4,4 +4,27 @@ export const formatCurrency = (amount:number) => {
const formattedAmount = (amount/100).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
return(formattedAmount);
}
/**
* Formats an IBAN for display with proper spacing
* Format: XX12 XXXX XXXX XXXX XXXX XX
* First group: 2 letters (country) + 2 digits (check) = 4 chars
* Following groups: 4 characters each
* Last group: can be less than 4 characters
* @param iban - IBAN string without spaces
* @returns Formatted IBAN with spaces
*/
export const formatIban = (iban: string | null | undefined): string => {
if (!iban) return "";
// Remove any existing spaces
const cleaned = iban.replace(/\s/g, '');
// Split into groups: first 4 chars, then groups of 4
const groups: string[] = [];
for (let i = 0; i < cleaned.length; i += 4) {
groups.push(cleaned.slice(i, i + 4));
}
return groups.join(' ');
}

View File

@@ -7,6 +7,7 @@ import { useFormState, useFormStatus } from "react-dom";
import { useLocale, useTranslations } from "next-intl";
import Link from "next/link";
import AccountCircleIcon from "@mui/icons-material/AccountCircle";
import { formatIban } from "../lib/formatStrings";
export type AccountFormProps = {
profile: UserProfile | null;
@@ -28,15 +29,16 @@ const FormFields: FC<FormFieldsProps> = ({ profile, errors, message }) => {
firstName: profile?.firstName ?? "",
lastName: profile?.lastName ?? "",
address: profile?.address ?? "",
iban: profile?.iban ?? "",
iban: formatIban(profile?.iban) ?? "",
});
const handleInputChange = (field: keyof typeof formValues, value: string) => {
setFormValues(prev => ({ ...prev, [field]: value }));
};
// Check if any required field is missing
const hasMissingData = !formValues.firstName || !formValues.lastName || !formValues.address || !formValues.iban;
// Check if any required field is missing (clean IBAN of spaces for validation)
const cleanedIban = formValues.iban.replace(/\s/g, '');
const hasMissingData = !formValues.firstName || !formValues.lastName || !formValues.address || !cleanedIban;
return (
<>
@@ -127,7 +129,7 @@ const FormFields: FC<FormFieldsProps> = ({ profile, errors, message }) => {
type="text"
placeholder={t("iban-placeholder")}
className="input input-bordered w-full"
defaultValue={profile?.iban ?? ""}
defaultValue={formatIban(profile?.iban)}
onChange={(e) => handleInputChange("iban", e.target.value)}
disabled={pending}
/>