diff --git a/packages/create-invoice-form/src/lib/invoice/form.svelte b/packages/create-invoice-form/src/lib/invoice/form.svelte index 3762f3c1..f58c0686 100644 --- a/packages/create-invoice-form/src/lib/invoice/form.svelte +++ b/packages/create-invoice-form/src/lib/invoice/form.svelte @@ -26,8 +26,17 @@ export let handleNetworkChange: (chainId: string) => void; export let networks; export let defaultCurrencies: any = []; - export let payeeAddressError = false; - export let clientAddressError = false; + + let validationErrors = { + payeeAddress: false, + clientAddress: false, + sellerInfo: { + email: false, + }, + buyerInfo: { + email: false, + }, + }; let creatorId = ""; @@ -35,12 +44,17 @@ creatorId = formData.creatorId; } + const validateEmail = (email: string, type: "sellerInfo" | "buyerInfo") => { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + validationErrors[`${type}`].email = !emailRegex.test(email); + }; + const checkPayeeAddress = () => { - payeeAddressError = !checkAddress(formData.payeeAddress); + validationErrors.payeeAddress = !checkAddress(formData.payeeAddress); }; const checkClientAddress = () => { - clientAddressError = !checkAddress(formData.payerAddress); + validationErrors.clientAddress = !checkAddress(formData.payerAddress); }; const calculateInputWidth = (value: string) => { @@ -177,7 +191,13 @@ type="email" value={formData.sellerInfo?.email} placeholder="Email" - handleInput={handleAdditionalInfo} + handleInput={(e) => { + handleAdditionalInfo(e); + }} + onBlur={(e) => validateEmail(e?.target?.value, "sellerInfo")} + error={validationErrors.sellerInfo.email + ? "Please enter a valid email" + : ""} /> - {#if clientAddressError} -

Please enter a valid Ethereum address

- {/if}
{ + handleAdditionalInfo(e); + }} + onBlur={(e) => validateEmail(e?.target?.value, "buyerInfo")} + error={validationErrors.buyerInfo.email + ? "Please enter a valid email" + : ""} /> - {#if payeeAddressError} -

Please enter a valid Ethereum address

- {/if}
diff --git a/shared/components/input.svelte b/shared/components/input.svelte index 053586ca..98edbfc3 100644 --- a/shared/components/input.svelte +++ b/shared/components/input.svelte @@ -14,12 +14,14 @@ export let max = 0; export let style = ""; export let width = ""; + export let error: string | null = null;
{#if label} {/if} +
@@ -33,7 +35,7 @@ {placeholder} maxlength={max} on:input={handleInput} - class={`textarea-input ${className}`} + class={`textarea-input ${className} ${error ? "input-error" : ""}`} /> {:else} {/if}
+ + {#if error?.length > 0} +

{error}

+ {/if}