validateEmail

validateEmail(s) normalizes s with toString and lowercases it, then returns that value when it looks like an email — at least 6 characters long and matching ^[^@]+@[^@]+\.[^@]+$. Anything else returns false. The returned string is the cleaned value, so you can store it directly.

The check is intentionally loose (one @, a dot in the domain); it is a sanity filter, not full RFC validation. Part of pota/use/string.

Arguments

Argument Type Description
s string The email to validate.

Returns: string | false — the lowercased, trimmed email when valid, otherwise false.

Examples

Validate on submit

Reads the field, validates it, and either reports the error or uses the normalized value.

import { render, signal } from 'pota'
import { validateEmail } from 'pota/use/string'

function App() {
	const raw = signal('')
	const error = signal('')
	const success = signal('')

	function submit(e) {
		e.preventDefault()
		const email = validateEmail(raw.read())
		if (email === false) {
			error.write('Please enter a valid email')
			success.write('')
		} else {
			error.write('')
			success.write(`normalized: ${email}`) // e.g. 'A@B.UY' -> 'a@b.uy'
		}
	}

	return (
		<form on:submit={submit}>
			<input
				prop:value={raw.read}
				on:input={e => raw.write(e.currentTarget.value)}
			/>
			<button>Submit</button>
			<p>{error.read}</p>
			<p>{success.read}</p>
		</form>
	)
}

render(App)