jQuery: The Write Less, Do More JavaScript Library

Plugins/Validation/rules

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Plugins/Validation

rules( )

Return the validations rules for the first selected element.
There are several ways to specify validation rules.
  • Validation methods without parameters can be specified as classes on the element (recommended)
  • Validation methods with parameters can be specified as attributes (recommended)
  • Both can be specified as metadata using the metadata plugin
  • Both can be specified using the rules-option of the validate()-method


Examples:

An example using classes (class="required") and attributes (minlength="2") to specify validation rules.

$("#myform").validate();

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/lib/jquery.delegate.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
  <script>
  $(document).ready(function(){
    $("#myform").validate();
  });
  </script>
  
</head>
<body>
  
<form id="myform">
  <input type="text" name="name" class="required" minlength="2" />
  <br/>
  <input type="submit" value="Submit"/>
</form>
</body>
</html>

NameType