|
1 |
| -# test |
| 1 | +This plugin was created to minimize javascript logic in the html code when dealing with front-end validation of form data. |
| 2 | + |
| 3 | +[Live example can be viewed here](http://victorjonsson.se/jquery-form-validator) |
| 4 | + |
| 5 | +[Also available for download at jQuery.com](http://plugins.jquery.com/project/jQueryFormValidatorMadeEasy) |
| 6 | + |
| 7 | +*Usage example* |
| 8 | + |
| 9 | +```html |
| 10 | +<form action="" onsubmit="return $(this).validate()"> |
| 11 | + <p> |
| 12 | + Name (4 characters minimum): |
| 13 | + <input name="user" data-validation="validate_min_length length4" /> |
| 14 | + </p> |
| 15 | + <p> |
| 16 | + Birthdate (yyyy-mm-dd): |
| 17 | + <input name="birth" data-validation="validate_birthdate" /> |
| 18 | + </p> |
| 19 | + <p> |
| 20 | + Website: |
| 21 | + <input name="website" data-validation="validate_url" /> |
| 22 | + </p> |
| 23 | + <p> |
| 24 | + <input type="submit" /> |
| 25 | + </p> |
| 26 | + </form> |
| 27 | +``` |
| 28 | + |
| 29 | +## Features |
| 30 | + * **validate_url** |
| 31 | + * **validate_date** — *yyyy-mm-dd (format can be customized, more information below)* |
| 32 | + * **validate_birthdate** — *yyyy-mm-dd, not allowing dates in the future or dates that is older then 122 years (format can be customized, more information below)* |
| 33 | + * **validate_email** |
| 34 | + * **validate_time** — *hh:mm* |
| 35 | + * **validate_domain** — *domain.com* |
| 36 | + * **validate_phone** — *atleast 7 digits only one hyphen and plus allowed* |
| 37 | + * **validate_swemob** — *validate that the value is a swedish mobile telephone number* |
| 38 | + * **validate_float** |
| 39 | + * **validate_int** |
| 40 | + * **validate_length** — *Validate that input length is in given range (length3-20)* |
| 41 | + * **validate_confirmation** |
| 42 | + * **validate_spamcheck** |
| 43 | + * **validate_swesec** - *validate swedish social security number* |
| 44 | + * **required** — *no validation except that a value has to be given* |
| 45 | + * **validate_custom** - *Validate value against regexp (validate_custom regexp/^[a-z]{2} [0-9]{2}$/) |
| 46 | + |
| 47 | + * Show help information automatically when input is focused |
| 48 | + * Validate given values immediately when input is blurred. |
| 49 | + |
| 50 | + |
| 51 | +## Validate inputs on blur |
| 52 | +It is now possible to show that the value of an input is incorrect immediately when the input gets blurred. |
| 53 | + |
| 54 | +```html |
| 55 | +<form action="" onsubmit="return $(this).validate();" id="my_form"> |
| 56 | + <p> |
| 57 | + <strong>Website:</strong> |
| 58 | + <input type="text" name="website" data-validation="validate_url" /> |
| 59 | + </p> |
| 60 | + ... |
| 61 | +</form> |
| 62 | + |
| 63 | +<script> |
| 64 | + $('#my_form').validateOnBlur(); |
| 65 | +</script> |
| 66 | +``` |
| 67 | + |
| 68 | +## Show help information |
| 69 | +Since version 1.1 it is possible to display help information for each input. The information will fade in when input is focused and fade out when input is blurred. |
| 70 | + |
| 71 | +```html |
| 72 | +<form action="" onsubmit="return $(this).validate();" id="my_form"> |
| 73 | + <p> |
| 74 | + <strong>Why not:</strong> |
| 75 | + <textarea name="why" data-help="Please give us some more information" data-validation="required"></textarea> |
| 76 | + </p> |
| 77 | + ... |
| 78 | +</form> |
| 79 | + |
| 80 | +<script> |
| 81 | + $('#my_form').showHelpOnFocus(); |
| 82 | +</script> |
| 83 | +``` |
| 84 | + |
| 85 | +## Fully customizable |
| 86 | +```javascript |
| 87 | +var myConf = { |
| 88 | + // Name of element attribute holding the validation rules (default is data-validation) |
| 89 | + validationRuleAttribute : 'class', |
| 90 | + |
| 91 | + // Names of inputs not to be validated even though the element attribute containing |
| 92 | + // the validation rules tells us to |
| 93 | + ignore : ['som-name', 'other-name'], |
| 94 | + |
| 95 | + // Class that will be put on elements which value is invalid (default is 'error') |
| 96 | + errorElementClass : 'error', |
| 97 | + |
| 98 | + // Border color of elements which value is invalid, empty string to leave border |
| 99 | + // color as it is |
| 100 | + borderColorOnError : '#FFF', |
| 101 | + |
| 102 | + // Class of div container showing error messages (defualt is 'error_message') |
| 103 | + errorMessageClass : 'error_message', |
| 104 | + |
| 105 | + // Position of error messages. Set the value to "top" if you want the error messages |
| 106 | + // to be displayed in the top of the form. Otherwise you can set the value to |
| 107 | + // "element", each error message will then be displayed beside the input field that |
| 108 | + // it is refering to (default is 'top') |
| 109 | + errorMessagePosition : 'element', |
| 110 | + |
| 111 | + // Date format used when validating dates and birthdate. (default is yyyy-mm-dd) |
| 112 | + dateFormat : 'dd/mm/yyyy', |
| 113 | + |
| 114 | + // Window automatically scrolls to the top of the form when submitted data is |
| 115 | + // invalid (default is true) |
| 116 | + scrollToTopOnError : false |
| 117 | +}; |
| 118 | + |
| 119 | +var myLang = { |
| 120 | + errorTitle : 'Något gick fel', |
| 121 | + requiredFields : 'Du fyllde inte i alla fält markerade med *' |
| 122 | +}; |
| 123 | + |
| 124 | +$('#my_form') |
| 125 | + .showHelpOnFocus() |
| 126 | + .validateOnBlur(myLang, myConf) |
| 127 | + .submit(function() { |
| 128 | + return $(this).validate(myLang, myConf); |
| 129 | + }); |
| 130 | +``` |
| 131 | + |
| 132 | +## Simple captcha example |
| 133 | +```php |
| 134 | +<?php |
| 135 | +session_start(); |
| 136 | +if( isset($_POST['captcha']) && isset($_SESSION['captcha'])) { |
| 137 | + if($_POST['captcha'] != ($_SESSION['captcha'][0]+$_SESSION['captcha'][1])) |
| 138 | + die('Invalid captcha answer'); // client does not have javascript enabled |
| 139 | +} |
| 140 | + |
| 141 | +$_SESSION['captcha'] = array( mt_rand(0,9), mt_rand(1, 9) ); |
| 142 | + |
| 143 | +?> |
| 144 | +<html> |
| 145 | +.... |
| 146 | +<form action="" onsubmit="return $(this).validate();"> |
| 147 | + <p> |
| 148 | + What is the sum of <?=$_SESSION['captcha'][0]?> + <?=$_SESSION['captcha'][1]?>? (security question) |
| 149 | + <input name="captcha" data-validation="validate_spamcheck captcha<?=( $_SESSION['capthca'][0] + $_SESSION['captcha'][1] )?>" |
| 150 | + </p> |
| 151 | + <p><input type="submit" /></p> |
| 152 | +</form> |
| 153 | +... |
| 154 | +</html> |
| 155 | +``` |
| 156 | + |
| 157 | +## Input length restriction |
| 158 | +```html |
| 159 | +<p> |
| 160 | + History (<span id="maxlength">50</span> characters left) |
| 161 | + <textarea rows="3" id="area"></textarea> |
| 162 | +</p> |
| 163 | +<script type="text/javascript"> |
| 164 | + $('#area').restrictLength($('#maxlength')); |
| 165 | +</script> |
| 166 | +``` |
| 167 | + |
| 168 | +## Password confirmation example |
| 169 | +```html |
| 170 | + <p>Password: <input type="password" name="pass" data-validation="validate_confirmation" /></p> |
| 171 | + <p>Confirm password: <input type="password" name="pass_confirmation" /></p> |
| 172 | +``` |
0 commit comments