Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to auto create the Describedby element #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions demo/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,31 @@ <h1>Basic Demo</h1>
</div>

<div class="control-group input-append">
<input type="text" name="age" id="age" data-required data-pattern="^[0-9]+$" />
<input type="text" name="age" id="age" data-required data-pattern="^[0-9]+$"
data-description="{&#34;required&#34;:&#34;This field is required&#34;,&#34;pattern&#34; : &#34;Numbers only&#34;}" data-describedby="age-description" />

<label for="age" class="add-on"><span class="icon-asterisk"></span> Age</label>

</div>

<div class="control-group input-append">
<label class="control-label">Quality Control</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="qualityControl[]" value="fast" data-required data-description="{&#34;required&#34;:&#34;This field is required&#34;}" data-describedby="chceck-description" >
Fast
</label>
<label class="checkbox">
<input type="checkbox" name="qualityControl[]" value="cheap" data-required data-description="{&#34;required&#34;:&#34;This field is required&#34;}" data-describedby="chceck-description" >
Cheap
</label>
<label class="checkbox">
<input type="checkbox" name="qualityControl[]" value="good" data-required data-description="{&#34;required&#34;:&#34;This field is required&#34;}" data-describedby="chceck-description" >
Good
</label>
</div>
</div>

<div class="btn-group">
<button type="submit" class="btn btn-primary">Send</button>

Expand All @@ -53,16 +73,19 @@ <h1>Basic Demo</h1>

<script>
$('form').validate({
onKeyup : true,
onBlur : true,
onChange : true,
eachValidField : function() {

$(this).closest('div').removeClass('error').addClass('success');
$(this).closest('label').removeClass('error').addClass('success');
},
eachInvalidField : function() {

$(this).closest('div').removeClass('success').addClass('error');
$(this).closest('label').removeClass('success').addClass('error');
}
});
</script>
</body>
</html>
</html>
4 changes: 4 additions & 0 deletions demo/vendor/bootstrap/css/bootstrap.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* Designed and built with all the love in the world @twitter by @mdo and @fat.
*/

label.error{color:#F00}
label.success{color:#0F0}
.validation-advice{color:#F00;font-size:14px;font-weight:bold}

article,
aside,
details,
Expand Down
769 changes: 768 additions & 1 deletion demo/vendor/bootstrap/css/bootstrap.min.css

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions jquery-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

var

type = ['input:not([type]),input[type="color"],input[type="date"],input[type="datetime"],input[type="datetime-local"],input[type="email"],input[type="file"],input[type="hidden"],input[type="month"],input[type="number"],input[type="password"],input[type="range"],input[type="search"],input[type="tel"],input[type="text"],input[type="time"],input[type="url"],input[type="week"],textarea', 'select', 'input[type="checkbox"],input[type="radio"]'],
//input:not([type]), this was causing errors on jQuery 1.8.1 (default jQuery in Joomla)
type = ['input[type="hidden"],input[type="color"],input[type="date"],input[type="datetime"],input[type="datetime-local"],input[type="email"],input[type="file"],input[type="hidden"],input[type="month"],input[type="number"],input[type="password"],input[type="range"],input[type="search"],input[type="tel"],input[type="text"],input[type="time"],input[type="url"],input[type="week"],textarea', 'select', 'input[type="checkbox"],input[type="radio"]'],

// All field types
allTypes = type.join(','),
Expand Down Expand Up @@ -207,8 +208,18 @@

log = fieldDescription.valid;

if(describedby.length > 0 && event.type != 'keyup') {
if(typeof(fieldDescribedby) !== "undefined" && options.autoDescribedby != false && describedby.length == 0){
describedby = $(options.autoDescribedby).attr('id',fieldDescribedby);
if(field.attr('type') == 'checkbox' || field.attr('type') == 'ratio'){
$(field).parent().parent().append(describedby); // The first parent in this case is usually the label
}else{
$(field).parent().append(describedby);
}

}

if(describedby.length > 0 && event.type != 'keyup') {
describedby.css('display','block');
if(!status.required) {

log = fieldDescription.required;
Expand All @@ -218,6 +229,8 @@
} else if(!status.conditional) {

log = fieldDescription.conditional;
} else if(!log){ // hide if there is no message
describedby.css('display','none');
}

describedby.html(log || '');
Expand Down Expand Up @@ -433,6 +446,9 @@
// Validate on onChange?
onChange : false,

// Create Describedby automatically is missing. Set to false to disable it
autoDescribedby : '<div class="validation-advice" />',

// Default namespace
nameSpace : 'validate',

Expand Down Expand Up @@ -462,4 +478,4 @@

// A fielter to the fields
filter : '*'
}, jQuery, window);
}, jQuery, window);