jValX
Notice:
- 2006-06-05: removed default invalid field highlighting - you now must specify a css class of "jvalx-invalid-field" and/or "jvalx-invalid-label" to have formatting applied to fields and labels respectively
- 2006-02-09: change to the signatures of jvalOverride() and jvalReset() is not backwards-compatible - you must pass the form to these methods now. See additional notes
topwhat is jValX?
jValX is an easy way to simplify your client-side form validation, and is all XHTML 1.0 STRICT markup.
topinstall in four easy steps:
- download jvalx.js by right-clicking here and choosing "Save as..." or "Save Link to Disk"
- include it on your page like so:
<script type="text/javascript" src="jvalx.js"></script> - add a "config" comment node adjacent to each form field (see examples below, additional details provided in jvalx.js)
- add
onsubmit="jValidate(this);"to your<form>tag.
the config nodes can even come before the element. just be sure to
nest them properly, i.e.
<field1><!-- config1 -->
<field2><!-- config2 -->
will work, but
<field1><!-- config2 -->
<!-- config1 --><field2>
will not.
topcreating config nodes:
setting up your config nodes is easy! they're really just
HTML comments with some
custom attributes added. only the for attribute is mandatory
- this maps the config node to it's respective form field. all other config node attributes are optional.
topconfig node attributes:
| attribute | value | description |
|---|---|---|
for |
string | the name of the input field this config node is for (this attribute is mandatory) |
condition |
any valid javascript | the condition to test for when required="conditional". the result of the condition determines if the field is required or not. (true=required) |
constraint |
any valid javascript that evaluates to or returns boolean | optional additional validation logic to be evaluated onsubmit. simply add code or a function that returns boolean, true meaning valid input. (see the "select with constraint" in the demo form below) |
constraintErrorMsg |
string | when a constraint is present, the error message to be displayed (if any) if the input's
value fails constraint validation. if omitted, defaults to value of errorMsg
|
dataType |
any valid dataType name | determines what type of input the field should accept |
errorMsg |
string | the error message to be displayed (if any) if the input's value fails validation. if omitted, defaults to "Please enter a valid value." |
equalOk |
true|false |
if dataType="dateRange", whether or not the two dates can be equal, default is false |
firstOk |
true|false |
if dataType="select" and required="true", whether or not the first option is an acceptable choice. e.g. if the first option is "Please Choose", then firstOk="false" would prevent this option from being selected. default is false. |
lenient |
true|false |
if dataType="decimal", whether or not to allow integers.
defaults to false if omitted.
|
lowerElement |
string | if dataType="dateRange", the name of the form element representing the upper date. |
max |
numeric | maximum numeric value or text length of the input's value, depending on it's dataType |
min |
numeric | minimum numeric value or text length of the input's value, depending on it's dataType |
regex |
any valid regular expression, minus the opening and closing regex delimiters "/" | a regular expression to test the input's value against. NOTE: opening and closing regex delimiters "/" are not necessary, and will be removed if present. |
regexIgnoreCase |
true|false |
when the regex attribute is present, whether it should be case-insensitive or not. |
required |
true|false|conditional |
whether or not the input must be given a value, default is false |
upperElement |
string | if dataType="dateRange", the name of the form element representing the lower date. |
topdataTypes:
| dataType | description |
|---|---|
| alpha | the field will accept any letter, space, hyphen or underscore as input |
| date | the field will accept U.S. format date: MM/DD/YYYY as input |
| decimal | the field will accept any decimal as input |
| the field will accept any valid email format as input | |
| integer | the field will accept any integer as input |
| phone | the field will accept U.S. format phone: x-xxx-xxx-xxxx as input |
| text | the field will accept any character as input |
| [null] | dataType attribute may be left blank or omitted altogether, and jValX will default to dataType "text" |
topadditional notes:
- multiple form submissions: by default, jValX prevents your form from submitting again
upon successful validation. if you wish to override this, simply pass
trueas the second argument to jValidate() in your form tag.
jValidate()'s signature is:jValidate(objectForm[, booleanAllowMultipleSubmissions]), and booleanAllowMultipleSubmissions defaults to false if omitted. jvalReset(this.form): callingjvalReset(this.form)after successful validation will also re-enable submission.-
jvalOverride(this.form);: If you have a Cancel button on your form that is of type="submit", you can abort the form's validation by attaching jvalOverride(this.form); to the button's onclick handler, e.g.<input type="submit" value="Cancel" onclick="jvalOverride(this.form);" />. - radios, checkboxes, etc: groups of elements with the same name need only one config node, provided that the validation rules are the same for all elements in the group. (see the "Pizza" radio group in the demo form below.)
topdemo form:
use the demo form below to watch jValX in action, and to see how easy it is to configure! (example config comment nodes are in green)
<form
action="javascript:alert('Validation successful');"
method="get"
onsubmit="return jValidate(this, true);">