Validation
Validate form fields using the sf-validate: directive. Add rules directly to your inputs - no JavaScript required.
Basic Usage
Section titled “Basic Usage”<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.
General Rules
Section titled “General Rules”required
Section titled “required”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" />active_url
Section titled “active_url”Must be a valid URL with an active DNS record.
<input name="website" sf-validate:active_url="URL must be active" />boolean
Section titled “boolean”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>accepted
Section titled “accepted”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" />Number Rules
Section titled “Number Rules”numeric
Section titled “numeric”Must be a valid number (1, 1.5, 0.5, 0).
<input name="price" sf-validate:numeric="Must be a number" />integer
Section titled “integer”Must be a whole number (1, 0, -1, 100).
<input name="quantity" sf-validate:integer="Must be a whole number" />max[value]
Section titled “max[value]”Must be less than or equal to value.
<input name="age" sf-validate:max[120]="Age cannot exceed 120" />min[value]
Section titled “min[value]”Must be greater than or equal to value.
<input name="age" sf-validate:min[18]="Must be 18 or older" />Length Rules
Section titled “Length Rules”max_length[value]
Section titled “max_length[value]”Maximum character length.
<textarea name="bio" sf-validate:max_length[500]="Bio too long (max 500 chars)"></textarea>min_length[value]
Section titled “min_length[value]”Minimum character length.
<input name="password" sf-validate:min_length[8]="Password must be at least 8 characters" />List Rules
Section titled “List Rules”in[value1,value2,…]
Section titled “in[value1,value2,…]”Must be one of the listed values.
<input name="color" sf-validate:in[red,green,blue]="Choose red, green, or blue" />not_in[value1,value2,…]
Section titled “not_in[value1,value2,…]”Must not be one of the listed values.
<input name="username" sf-validate:not_in[admin,root,system]="Username not available" />starts_with[value1,value2,…]
Section titled “starts_with[value1,value2,…]”Must start with one of the values.
<input name="phone" sf-validate:starts_with[+1,+44]="Use country code +1 or +44" />doesnt_start_with[value1,value2,…]
Section titled “doesnt_start_with[value1,value2,…]”Must not start with any of the values.
<input name="username" sf-validate:doesnt_start_with[admin]="Cannot start with admin" />doesnt_end_with[value1,value2,…]
Section titled “doesnt_end_with[value1,value2,…]”Must not end with any of the values.
<input name="email" sf-validate:doesnt_end_with[.test,.example]="Use a real email domain" />Date Rules
Section titled “Date Rules”Must be a valid date.
<input type="date" name="birthday" sf-validate:date="Invalid date" />after[date]
Section titled “after[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" />before[date]
Section titled “before[date]”Must be before the specified date.
<input type="date" name="end" sf-validate:before[2030-01-01]="Must be before 2030" />date_equals[date]
Section titled “date_equals[date]”Must equal the specified date.
<input type="date" name="confirm" sf-validate:date_equals[2024-12-25]="Enter Dec 25, 2024" />Field Comparison Rules
Section titled “Field Comparison Rules”same[field]
Section titled “same[field]”Must match another field’s value.
<input name="password" type="password" /><input name="password_confirm" type="password" sf-validate:same[password]="Passwords must match" />gt[field]
Section titled “gt[field]”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" />gte[field]
Section titled “gte[field]”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" />lt[field]
Section titled “lt[field]”Must be less than another field.
<input name="max_budget" /><input name="bid" sf-validate:lt[max_budget]="Bid must be under budget" />lte[field]
Section titled “lte[field]”Must be less than or equal to another field.
<input name="available" /><input name="quantity" sf-validate:lte[available]="Cannot exceed available stock" />Type Rules
Section titled “Type Rules”Only letters allowed.
<input name="name" sf-validate:alpha="Letters only" />alpha_dash
Section titled “alpha_dash”Only letters, numbers, dashes, and underscores.
<input name="slug" sf-validate:alpha_dash="Letters, numbers, dashes, underscores only" />alpha_num
Section titled “alpha_num”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" />Combining Rules
Section titled “Combining Rules”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.