Skip to content

Commit

Permalink
#64: Support Danish zip code
Browse files Browse the repository at this point in the history
  • Loading branch information
nghuuphuoc committed Mar 6, 2014
1 parent c98619e commit 1f66ea7
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 39 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

## v0.3.0

__New features__:

* [#44: Rewrite entirely using Deferred](https://github.com/nghuuphuoc/bootstrapvalidator/issues/44)
* [#64: Support Danish zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
* [#71: Show all errors](https://github.com/nghuuphuoc/bootstrapvalidator/issues/71)

__Fixes__:

* [#51: Submit after submit doesn't work](https://github.com/nghuuphuoc/bootstrapvalidator/issues/51)
* [#53: Fix notEmpty validator for radios and checkboxes](https://github.com/nghuuphuoc/bootstrapvalidator/issues/53)
* [#62: The callback validator passes wrong parameter](https://github.com/nghuuphuoc/bootstrapvalidator/issues/62)

Document:
__Document__:

* [#60: Update the installation guide](https://github.com/nghuuphuoc/bootstrapvalidator/pull/60)
* [#73: Describe which version should be included in the Usage section](https://github.com/nghuuphuoc/bootstrapvalidator/issues/73)

Expand Down
7 changes: 4 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module.exports = function(grunt) {

banner: [
'/**',
' * BootstrapValidator v<%= pkg.version %> (<%= pkg.homepage %>)',
' * BootstrapValidator (<%= pkg.homepage %>)',
' *',
' * A jQuery plugin to validate form fields. Use with Bootstrap 3',
' *',
' * @author Nguyen Huu Phuoc <[email protected]>',
' * @copyright (c) 2013 Nguyen Huu Phuoc',
' * @version v<%= pkg.version %>',
' * @author <%= pkg.author.url %>',
' * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc',
' * @license MIT',
' */\n\n'
].join('\n'),
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ notEmpty | Check if the value is empty
[remote](#remote-validator) | Perform remote checking via Ajax request
[stringLength](#stringlength-validator) | Validate the length of a string
uri | Validate an URL address
usZipCode | Validate a US zip code
[zipCode](#zipcode-validator) | Validate a zip code

The validator options are described in the following section:

Expand Down Expand Up @@ -221,6 +221,15 @@ message | n/a | The error message
min | n/a | The minimum length
max | n/a | The maximum length. One of ```min```, ```max``` options is required

### ZipCode Validator

| Option name | Default | Description
| ------------|---------|------------
| message | n/a | The error message
| country | US | A ISO 3166 country code. Currently it supports the following countries:
| | | - US (United State)
| | | - DK (Denmark)

## Write new validator

A validator has to follow the syntax:
Expand Down Expand Up @@ -310,7 +319,7 @@ This software is written by Nguyen Huu Phuoc, aka @nghuuphuoc

Big thanks to the contributor:

* Vu Minh Khang, aka [@khangvm53](https://github.com/khangvm53)
[@khangvm53](https://github.com/khangvm53)

## License

Expand Down
2 changes: 1 addition & 1 deletion demo/remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// and open the remote.html file:
// http://domain.com/demo/remote.html

sleep(5);
//sleep(5);

$valid = true;

Expand Down
3 changes: 2 additions & 1 deletion demo/validators.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ <h1>Bootstrap Validate plugin</h1>
},
zipCode: {
validators: {
usZipCode: {
zipCode: {
country: 'US',
message: 'The input is not a valid US zip code'
}
}
Expand Down
22 changes: 17 additions & 5 deletions dist/js/bootstrapValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,21 +826,33 @@
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.usZipCode = {
$.fn.bootstrapValidator.validators.zipCode = {
/**
* Return true if and only if the input value is a valid US zip code
* Return true if and only if the input value is a valid country zip code
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @param {Object} options Consist of key:
* - country: The ISO 3166 country code
*
* Currently it supports the following countries:
* - US (United State)
* - DK (Denmark)
*
* @returns {Boolean}
*/
validate: function(validateInstance, $field, options) {
var value = $field.val();
if (value == '') {
if (value == '' || !options.country) {
return true;
}
return /^\d{5}([\-]\d{4})?$/.test(value);
switch (options.country.toUpperCase()) {
case 'DK':
return /^(DK(-|\s)?)?\d{4}$/i.test(value);
case 'US':
default:
return /^\d{5}([\-]\d{4})?$/.test(value);
}
}
};
}(window.jQuery));
Loading

0 comments on commit 1f66ea7

Please sign in to comment.