Jump to : setup | field validation | error message customization
The purpose of the input validation in wForms is to help users fill out a form correctly, by defining required fields and allowed formats and by providing appropriate error messages when the condition for submission are not met.
Please note that client-side validation cannot guarantee that form fields are correctly formatted upon submission. Server-side validation is always necessary.
Retrieve the wForms files and enable the wForms extension by adding the following code to the <head> element of your page:
<script type="text/javascript" src="wforms_core.js"></script>
<script type="text/javascript" src="wforms_validation.js"></script>
By default the validation runs when the submit button is pressed. You can also choose to trigger the validation as soon as the user moves to the next field. To do so, simply add the following code:
<script type="text/javascript" src="wforms_onblur_validation.js"></script>
Note: onBlur validation is not used in the examples below.
To enable the validation you only need to add one of the following class to the your input fields.
<input type="text" ... class="required"/>
You may use required in combination with any other class. For instance,
<input type="text" ... class="validate-alpha required"/>
You may place required on an input's parent element, such as a div or fieldset. In this configuration, at least one field must be filled. To requires all fields, either place required on each field or see allrequired.
<div class="required">
<input type="text" ... />
<input type="text" ... />
</div>
<input type="text" ... class="validate-alpha"/>
<input type="text" ... class="validate-alphanum"/>
<input type="text" ... class="validate-date"/>
<input type="text" ... class="validate-email"/>
<input type="text" ... class="validate-integer"/>
<input type="text" ... class="validate-float"/>
<input type="text" ... class="validate-custom /\d\d/"/>
<input type="text" ... class="validate-customif /\d\d/"/>
<div class="allrequired">
<select ... >...</select>
<input type="radio" value="choice1" ... />
...
</div>
Error messages are automatically inserted in your form when an error is detected.
The following classes are applied to the elements in error. Use CSS to change their appearance:
<style type="text/css">
.errFld {border: 1px solid #F00; /*... or any other css properties ... */}
.errMsg { color: #C33; /*... or any other css properties ... */ }
</style>
You can change the text of the message by overwriting the following properties of the javascript wFORMS object.
<script type="text/javascript" src="wforms_core.js" ></script>
<script type="text/javascript" src="wforms_validation.js" ></script>
<script type="text/javascript">
wFORMS.behaviors['validation'].errMsg_notification = "%% error(s) detected. Your form has not been submitted yet.\nPlease check the information you provided."; // %% will be replaced by the actual number of errors
wFORMS.behaviors['validation'].errMsg_required = "This field is required";
</script>
You can change the default placement of the error message (just after the field) by having placeholders for each field. A placeholder is a div with an id attribute set according to this convention: field_id + '-E'
<div id="myinput-E"></div>
<input type="text" id="myinput" .../>
Here, the error message will be displayed above the field in error.
You can disable the alert box that appears when errors are detected.
<script type="text/javascript">
wFORMS.showAlertOnError = false; /* or true */
</script>
As of wForms v2.0 R42, the validation removes switched-off fields before submission (see Switch Behavior).
If you still want to submit all form fields, simply add the following script to your form.
<script type="text/javascript">
wFORMS.behaviors.validation.submitSwitchedOffFields = false;
</script>
wForms is an open-source, unobtrusive javascript library that adds commonly needed behaviors to traditional web forms without the need for any programming skill.