b:rule

<b:rule />

Sets a validation rule for a form field

<b:rule /> sets a new validation rule for a form field. You can do this by placing validation tag inside the field tag inside <b:form> or by referring to a single form and a single field with the tag name. See the example for setting validation rules.

Rules are set by defining test or regexp attribute.

Attributes

You can't use both of these attributes in the same tag. If you use both, test attribute will be used.

test

(optional)

Tests the value of the field against a condition.

Format for the test attribute is "[operator] [operand]". You don't need operand with all of the operators.

Valid operators for condition are:

longer than
test if field has more characters than the given number
shorter than
test if field has less characters than the given number
bigger than
tests if the value is bigger than the given number
smaller than
tests if the value is smaller than the given number
notempty
tests that the field is not empty
only
tests that the field contains only a given type of value

With only operator you can choose from the following types:

  • letters - only letters between a and z (case insensitive)
  • numbers
  • text - letters, numbers and spaces
  • email
  • url

If you need to have a more specific rule, use the regexp attribute instead.

regexp

(optional)

The regexp attribute uses PCRE regex syntax (PHP manual) for matching the value of the field.

The expression must be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash.

You can use pattern modifiers (PHP manual) after the closing delimiter.

You can learn more about regular expressions at regular-expressions.info.

message

Text that will be shown in <b:validate> if the given value doesn't match the rule

Examples

Setting validation rules

You can set a validation rule by placing a b:rule tag inside a field tag in b:form.

<b:form datamodel="comment" name="addcomment">
	<!--
		put name field to a form and set validation rule for it
	-->
	<label for="addcommentname">Your name:</label>
	<b:addcomment.name>
		<b:rule test="notempty" message="Please fill in your name"/>
	</b:addcomment.name>

	<!--
		And the same for email.

		If the email is empty, Bildy will stop validating this field
		and won't display multiple messages.
	-->
	<label for="addcommentemail">Your name:</label>
	<b:addcomment.email>
		<b:rule test="notempty" message="Please fill in your name"/>
		<b:rule test="only email"
		message="Please check that you entered your email address correctly"/>
	</b:addcomment.email>

	<input type="submit" value="Post a comment"/>
</b:form>

Using regular expressions

With regular expressions you can match field values precisely as you want.

<b:form datamodel="contacts" name="info">
	<!--
		Match first and last name that contain only letters a-z.

		Here we use the modifier i to make the regular expression
		case insensitive.
	-->
	<label for="infoname">Full name:</label>
	<b:info.name>
		<b:rule regexp="/^[a-z]+ [a-z]+$/i" message="Please fill in your full name"/>
	</b:info.name>

	<!--
		Match a valid North American phone number

		In this rule we decided to use the # delimiter instead of the typical /.
		You can use any non-alphanumeric and non-\ delimiter but if you need to 
		use the delimiter in the regular expression, you must escape it with \.
	-->
	<label for="infophone">Phone number:</label>
	<b:info.phone>
		<b:rule regexp="#\(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b#"
			message="Please fill in a valid phone number"/>
	</b:info.phone>

	<input type="submit" value="Send"/>
</b:form>