The field-type validations

Version: 9.0

A good web form usually contains one or more validations for one or more of its fields. A validation is a programmatically hard-coded condition that specifies whether the text you enter in a field of a web form can be accepted as a valid value. If the text entered does not conform to the predefined format, an error message is displayed in the web form field.

In the Web Forms for Marketers module, validations on a field type apply to all forms that use fields of that type (for example, Single-Line text) and these change if you change the field type, whereas form-specific validations stay with the field regardless of field-type.

The module supports web forms (.aspx) and MVC (.cshtml) layouts. Under field-type validations, both web forms and MVC are implemented in different ways. For web forms, a field type validation consists of a code and items that are stored in the content tree, in the validation item sitecore/system/modules/Web Forms for Marketers/settings. For MVC, a field type validation consists only of a code and does not use the validation item structure. Each field type contains predefined built-in validations relevant for the particular field type, for example: email, phone number, or credit card validations.

You can add a new, custom field type validation or custom form-specific field validation to a form, you can assign more than one validation to a field type, or you can remove them according to your needs.

Field-type validations for web forms:

Validation

Description

Used by field type

Count chars

Checks the number of characters in a string. You can set the minimum and maximum number of characters.

Single-Line Text

Multiple-Line Text

Password

Password-Confirmation

Date

Checks whether the value entered is a date.

Date

Email

Checks whether the value entered uses the format of an email address.

Email

Is Iso Date

Checks whether the value entered is an ISO date.

Number

Checks whether the values entered are numbers (negative numbers and integers are allowed).

Number

Number range

Checks whether the values entered are within a specified range of numbers.

Number

Regex pattern

Checks whether the values entered conform to a rule that you specify.

Single-Line Text

Multiple-Line Text

Password

Password-Confirmation

Telephone

Checks whether the value entered is a valid telephone number.

Telephone

SMS/MMS Telephone

Checks whether the value entered is a valid format for a text message or an MMS.

SMS/MMS Telephone

Luhn formula

American Express, Diners Club Carte Blanche, Diners Club International, Diners Club US and Canada, JCB, Maestro, MasterCard, Solo, Switch, Visa, Visa Electron

Checks the validity of a credit card number based on the type of credit card.

Credit card

Compare Password - Confirmation

Compares the values entered in the Password and Confirmation fields.

Password-Confirmation

Captcha

Compares the text displayed in a CAPTCHA field with the value entered by the user.

Captcha

Robot Protection

Checks whether a current visitor is a robot (using the robot detection algorithm).

Captcha

Suspicious Visitor

Identifies a website visitor who submits a web form several times during a short period of time as suspicious.

Captcha

Suspicious Form Activity

Identifies the form activity as suspicious if a web form is submitted several times during a short period of time by one or many users.

Captcha

Field-type validation attributes for MVC:

Description

Model name

Attributes

Checks the validity of a credit card number based on the type of credit card.

CreditCardField

RequestResponse
[CreditCard]
public override string Value { get; set; }

Checks whether or not the value entered is a date.

DateField

RequestResponse
[ParameterName("SelectedDate")]
public override string Value

Checks whether or not the value entered uses the format of an email address.

EmailField

RequestResponse
[DynamicEmail("EmailRegularExpression")]
[DataType(DataType.EmailAddress)]
public override string Value { get; set; }

Checks whether or not the values entered are numbers (negative numbers and integers are allowed). Also checks whether or not the values entered are within a specified range of numbers.

NumberField

RequestResponse
[RegularExpression(@"^[-,+]{0,1}\d*\.{0,1}\d+$", ErrorMessage = @"Field contains an invalid number.")]
[DynamicRange("MinimumValue", "MaximumValue", ErrorMessage = @"The number in {0} must be at least {1} and no more than {2}.")]
public override string Value { get; set; }

Compares the values entered in the Password and Confirmation fields.

PasswordConfirmationField

RequestResponse
[DataType(DataType.Password)]
[DynamicCompare("Value", "PasswordTitle", "ConfirmationTitle", ErrorMessage = @"The {0} and {1} fields must be the same.")]
public string Confirmation { get; set; }

Compares the text displayed on an image or sound voiced by a speaker with the value entered by the user.

RecaptchaField

RequestResponse
[RequestFormValue("g-recaptcha-response")]
[RecaptchaResponseValidator(ParameterName = "RecaptchaValidatorError")]
public override string Value { get; set; }

Checks whether or not the text in a field is within the specified min and max length.

SingleLineTextField

RequestResponse
[DataType(DataType.Text)]
[DynamicStringLength("MinLength", "MaxLength", ErrorMessage = @"The field {0} must be a string with a minimum length of {1} and a maximum length of {2}.")]
public override string Value { get; set; }

Checks whether or not the text is an SMS number

SmsTelephoneField

RequestResponse
[DynamicRegularExpression(@"^\+?\d{3,}", null, ErrorMessage = @"{0} contains an invalid sms/mms telephone number.")]
public override string Value { get; set; }

Checks whether or not the text is a phone number

TelephoneField

RequestResponse
[DataType(DataType.PhoneNumber)]    [DynamicRegularExpression(@"^\+?\s{0,}\d{0,}\s{0,}(\(\s{0,}\d{1,}\s{0,}\)\s{0,}|\d{0,}\s{0,}-?\s{0,})\d{2,}\s{0,}-?\s{0,}\d{2,}\s{0,}(-?\s{0,}\d{2,}|\s{0,})\s{0,}$", null, ErrorMessage = "{0} contains an invalid telephone number.")]
public override string Value { get; set; }

Do you have some feedback for us?

If you have suggestions for improving this article,