Skip to content

Validation

Validate form fields using the sf-validate: directive. Add rules directly to your inputs - no JavaScript required.

<input type="email" name="email"
sf-validate:required="Email is required"
sf-validate:email="Please enter a valid email" />

Rules are checked in order. If required fails, that error shows. If it passes, email is checked.


Field must have a value.

<input name="name" sf-validate:required="Name is required" />

Must be a valid email address.

<input name="email" sf-validate:email="Invalid email address" />

Must be a valid URL.

<input name="website" sf-validate:url="Please enter a valid URL" />

Must be a valid URL with an active DNS record.

<input name="website" sf-validate:active_url="URL must be active" />

Must be true, false, 1, or 0.

<select name="subscribe" sf-validate:boolean="Please select yes or no">
<option value="">Select...</option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>

Must be “yes”, “on”, 1, or true. For checkboxes/terms.

<input type="checkbox" name="terms" value="yes"
sf-validate:accepted="You must accept the terms" />

Must be a valid number (1, 1.5, 0.5, 0).

<input name="price" sf-validate:numeric="Must be a number" />

Must be a whole number (1, 0, -1, 100).

<input name="quantity" sf-validate:integer="Must be a whole number" />

Must be less than or equal to value.

<input name="age" sf-validate:max[120]="Age cannot exceed 120" />

Must be greater than or equal to value.

<input name="age" sf-validate:min[18]="Must be 18 or older" />

Maximum character length.

<textarea name="bio" sf-validate:max_length[500]="Bio too long (max 500 chars)"></textarea>

Minimum character length.

<input name="password" sf-validate:min_length[8]="Password must be at least 8 characters" />

Must be one of the listed values.

<input name="color" sf-validate:in[red,green,blue]="Choose red, green, or blue" />

Must not be one of the listed values.

<input name="username" sf-validate:not_in[admin,root,system]="Username not available" />

Must start with one of the values.

<input name="phone" sf-validate:starts_with[+1,+44]="Use country code +1 or +44" />

Must not start with any of the values.

<input name="username" sf-validate:doesnt_start_with[admin]="Cannot start with admin" />

Must not end with any of the values.

<input name="email" sf-validate:doesnt_end_with[.test,.example]="Use a real email domain" />

Must be a valid date.

<input type="date" name="birthday" sf-validate:date="Invalid date" />

Must be after the specified date. Supports today, tomorrow, yesterday.

<input type="date" name="start" sf-validate:after[today]="Must be a future date" />

Must be before the specified date.

<input type="date" name="end" sf-validate:before[2030-01-01]="Must be before 2030" />

Must equal the specified date.

<input type="date" name="confirm" sf-validate:date_equals[2024-12-25]="Enter Dec 25, 2024" />

Must match another field’s value.

<input name="password" type="password" />
<input name="password_confirm" type="password"
sf-validate:same[password]="Passwords must match" />

Must be greater than another field.

<input name="min_price" />
<input name="max_price" sf-validate:gt[min_price]="Max must be greater than min" />

Must be greater than or equal to another field.

<input name="start_year" />
<input name="end_year" sf-validate:gte[start_year]="End year must be >= start year" />

Must be less than another field.

<input name="max_budget" />
<input name="bid" sf-validate:lt[max_budget]="Bid must be under budget" />

Must be less than or equal to another field.

<input name="available" />
<input name="quantity" sf-validate:lte[available]="Cannot exceed available stock" />

Only letters allowed.

<input name="name" sf-validate:alpha="Letters only" />

Only letters, numbers, dashes, and underscores.

<input name="slug" sf-validate:alpha_dash="Letters, numbers, dashes, underscores only" />

Only letters and numbers.

<input name="code" sf-validate:alpha_num="Letters and numbers only" />

Must be a valid IP address (v4 or v6).

<input name="server_ip" sf-validate:ip="Invalid IP address" />

Must be a valid IPv4 address.

<input name="ipv4" sf-validate:ipv4="Invalid IPv4 address" />

Must be a valid IPv6 address.

<input name="ipv6" sf-validate:ipv6="Invalid IPv6 address" />

Must be a valid UUID.

<input name="id" sf-validate:uuid="Invalid UUID format" />

Add multiple rules to a single field:

<input name="username"
sf-validate:required="Username required"
sf-validate:alpha_dash="Only letters, numbers, dashes allowed"
sf-validate:min_length[3]="At least 3 characters"
sf-validate:max_length[20]="Max 20 characters" />

Rules are validated in order. First failure stops and shows that error.