diff --git a/app/views/partials/_answer.html.haml b/app/views/partials/_answer.html.haml
index 6b651afb..93b37123 100644
--- a/app/views/partials/_answer.html.haml
+++ b/app/views/partials/_answer.html.haml
@@ -14,12 +14,15 @@
= ff.input :answer_id, :as => :surveyor_check_boxes, :collection => [[a.text_for(nil, @render_context, I18n.locale), a.id]], :label => false, :input_html => {:class => a.css_class, :disabled => disabled}, :response_class => a.response_class
- when "none"
- if %w(date datetime time float integer string text).include? a.response_class
- = ff.input :answer_id, :as => :quiet, :input_html => {:class => a.css_class, :value => a.id}
+ - css_class = "#{a.css_class} surveyor_#{a.response_class}_answer"
+ - if q.mandatory?
+ - css_class << " surveyor_required"
+ = ff.input :answer_id, :as => :quiet, :input_html => {:class => css_class, :value => a.id}
= ff.input rc_to_attr(a.response_class),
:as => rc_to_as(a.response_class),
:label => a.text_for(:pre, @render_context, I18n.locale).blank? ? false : a.text_for(:pre, @render_context, I18n.locale),
:hint => a.text_for(:post, @render_context, I18n.locale),
- :input_html => generate_pick_none_input_html(r.to_formatted_s, a.default_value_for(@render_context, I18n.locale), a.css_class, a.response_class, disabled, a.input_mask, a.input_mask_placeholder)
+ :input_html => generate_pick_none_input_html(r.to_formatted_s, a.default_value_for(@render_context, I18n.locale), css_class, a.response_class, disabled, a.input_mask, a.input_mask_placeholder)
- else
= a.text_for(nil, @render_context, I18n.locale)
%span.help= a.help_text_for(@render_context, I18n.locale) unless g && g.display_type == "grid"
diff --git a/features/validations.feature b/features/validations.feature
new file mode 100644
index 00000000..0351199b
--- /dev/null
+++ b/features/validations.feature
@@ -0,0 +1,69 @@
+Feature: Survey with validations
+ As a survey participant
+ I want to take a survey
+ And be notified when I answer incorrectly
+
+ # Issue 34 - client side validation for mandatory question
+ @javascript
+ Scenario: Creating a mandatory question
+ Given I parse
+ """
+ survey "Mandatory Question" do
+ section "Required" do
+ q "Type your name. This is required.", :is_mandatory => true
+ a "name", :string
+ end
+ end
+ """
+ When I start the "Mandatory Question" survey
+ And I press "Click here to finish"
+ Then I should see "This field is required."
+
+ @javascript
+ Scenario: Creating a question with an integer answer
+ Given I parse
+ """
+ survey "Integer Question" do
+ section "How many" do
+ q "How many pets do you own?"
+ a "Number", :integer
+ end
+ end
+ """
+ When I start the "Integer Question" survey
+ And I fill in "Number" with "Eight"
+ And I press "Click here to finish"
+ Then I should see "A positive or negative non-decimal number please"
+
+ @javascript
+ Scenario: Creating a question with an float answer
+ Given I parse
+ """
+ survey "Float Question" do
+ section "How many" do
+ q "How mmuch oil do you use?"
+ a "Quantity", :float
+ end
+ end
+ """
+ When I start the "Float Question" survey
+ And I fill in "Quantity" with "A lot"
+ And I press "Click here to finish"
+ Then I should see "Please enter a valid number."
+
+ @javascript
+ Scenario: Creating a question with a time answer
+ Given I parse
+ """
+ survey "Time Question" do
+ section "When" do
+ q "What time is it?"
+ a "Time", :time
+ end
+ end
+ """
+ When I start the "Time Question" survey
+ And I press "When"
+ And I fill in "Time" with "0900"
+ When I press "Click here to finish"
+ Then I should see "Please enter a valid time, between 00:00 and 23:59"
diff --git a/lib/assets/javascripts/surveyor/jquery.validate.additional-methods.js b/lib/assets/javascripts/surveyor/jquery.validate.additional-methods.js
new file mode 100755
index 00000000..b0534b54
--- /dev/null
+++ b/lib/assets/javascripts/surveyor/jquery.validate.additional-methods.js
@@ -0,0 +1,617 @@
+/*!
+ * jQuery Validation Plugin 1.11.1
+ *
+ * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
+ * http://docs.jquery.com/Plugins/Validation
+ *
+ * Copyright 2013 Jörn Zaefferer
+ * Released under the MIT license:
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+(function() {
+
+ function stripHtml(value) {
+ // remove html tags and space chars
+ return value.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' ')
+ // remove punctuation
+ .replace(/[.(),;:!?%#$'"_+=\/\-]*/g,'');
+ }
+ jQuery.validator.addMethod("maxWords", function(value, element, params) {
+ return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length <= params;
+ }, jQuery.validator.format("Please enter {0} words or less."));
+
+ jQuery.validator.addMethod("minWords", function(value, element, params) {
+ return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params;
+ }, jQuery.validator.format("Please enter at least {0} words."));
+
+ jQuery.validator.addMethod("rangeWords", function(value, element, params) {
+ var valueStripped = stripHtml(value);
+ var regex = /\b\w+\b/g;
+ return this.optional(element) || valueStripped.match(regex).length >= params[0] && valueStripped.match(regex).length <= params[1];
+ }, jQuery.validator.format("Please enter between {0} and {1} words."));
+
+}());
+
+jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) {
+ return this.optional(element) || /^[a-z\-.,()'"\s]+$/i.test(value);
+}, "Letters or punctuation only please");
+
+jQuery.validator.addMethod("alphanumeric", function(value, element) {
+ return this.optional(element) || /^\w+$/i.test(value);
+}, "Letters, numbers, and underscores only please");
+
+jQuery.validator.addMethod("lettersonly", function(value, element) {
+ return this.optional(element) || /^[a-z]+$/i.test(value);
+}, "Letters only please");
+
+jQuery.validator.addMethod("nowhitespace", function(value, element) {
+ return this.optional(element) || /^\S+$/i.test(value);
+}, "No white space please");
+
+jQuery.validator.addMethod("ziprange", function(value, element) {
+ return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value);
+}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");
+
+jQuery.validator.addMethod("zipcodeUS", function(value, element) {
+ return this.optional(element) || /\d{5}-\d{4}$|^\d{5}$/.test(value);
+}, "The specified US ZIP Code is invalid");
+
+jQuery.validator.addMethod("integer", function(value, element) {
+ return this.optional(element) || /^-?\d+$/.test(value);
+}, "A positive or negative non-decimal number please");
+
+/**
+ * Return true, if the value is a valid vehicle identification number (VIN).
+ *
+ * Works with all kind of text inputs.
+ *
+ * @example
+ * @desc Declares a required input element whose value must be a valid vehicle identification number.
+ *
+ * @name jQuery.validator.methods.vinUS
+ * @type Boolean
+ * @cat Plugins/Validate/Methods
+ */
+jQuery.validator.addMethod("vinUS", function(v) {
+ if (v.length !== 17) {
+ return false;
+ }
+ var i, n, d, f, cd, cdv;
+ var LL = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y","Z"];
+ var VL = [1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9];
+ var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];
+ var rs = 0;
+ for(i = 0; i < 17; i++){
+ f = FL[i];
+ d = v.slice(i,i+1);
+ if (i === 8) {
+ cdv = d;
+ }
+ if (!isNaN(d)) {
+ d *= f;
+ } else {
+ for (n = 0; n < LL.length; n++) {
+ if (d.toUpperCase() === LL[n]) {
+ d = VL[n];
+ d *= f;
+ if (isNaN(cdv) && n === 8) {
+ cdv = LL[n];
+ }
+ break;
+ }
+ }
+ }
+ rs += d;
+ }
+ cd = rs % 11;
+ if (cd === 10) {
+ cd = "X";
+ }
+ if (cd === cdv) {
+ return true;
+ }
+ return false;
+}, "The specified vehicle identification number (VIN) is invalid.");
+
+/**
+ * Return true, if the value is a valid date, also making this formal check dd/mm/yyyy.
+ *
+ * @example jQuery.validator.methods.date("01/01/1900")
+ * @result true
+ *
+ * @example jQuery.validator.methods.date("01/13/1990")
+ * @result false
+ *
+ * @example jQuery.validator.methods.date("01.01.1900")
+ * @result false
+ *
+ * @example
+ * @desc Declares an optional input element whose value must be a valid date.
+ *
+ * @name jQuery.validator.methods.dateITA
+ * @type Boolean
+ * @cat Plugins/Validate/Methods
+ */
+jQuery.validator.addMethod("dateITA", function(value, element) {
+ var check = false;
+ var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
+ if( re.test(value)) {
+ var adata = value.split('/');
+ var gg = parseInt(adata[0],10);
+ var mm = parseInt(adata[1],10);
+ var aaaa = parseInt(adata[2],10);
+ var xdata = new Date(aaaa,mm-1,gg);
+ if ( ( xdata.getFullYear() === aaaa ) && ( xdata.getMonth() === mm - 1 ) && ( xdata.getDate() === gg ) ){
+ check = true;
+ } else {
+ check = false;
+ }
+ } else {
+ check = false;
+ }
+ return this.optional(element) || check;
+}, "Please enter a correct date");
+
+/**
+ * IBAN is the international bank account number.
+ * It has a country - specific format, that is checked here too
+ */
+jQuery.validator.addMethod("iban", function(value, element) {
+ // some quick simple tests to prevent needless work
+ if (this.optional(element)) {
+ return true;
+ }
+ if (!(/^([a-zA-Z0-9]{4} ){2,8}[a-zA-Z0-9]{1,4}|[a-zA-Z0-9]{12,34}$/.test(value))) {
+ return false;
+ }
+
+ // check the country code and find the country specific format
+ var iban = value.replace(/ /g,'').toUpperCase(); // remove spaces and to upper case
+ var countrycode = iban.substring(0,2);
+ var bbancountrypatterns = {
+ 'AL': "\\d{8}[\\dA-Z]{16}",
+ 'AD': "\\d{8}[\\dA-Z]{12}",
+ 'AT': "\\d{16}",
+ 'AZ': "[\\dA-Z]{4}\\d{20}",
+ 'BE': "\\d{12}",
+ 'BH': "[A-Z]{4}[\\dA-Z]{14}",
+ 'BA': "\\d{16}",
+ 'BR': "\\d{23}[A-Z][\\dA-Z]",
+ 'BG': "[A-Z]{4}\\d{6}[\\dA-Z]{8}",
+ 'CR': "\\d{17}",
+ 'HR': "\\d{17}",
+ 'CY': "\\d{8}[\\dA-Z]{16}",
+ 'CZ': "\\d{20}",
+ 'DK': "\\d{14}",
+ 'DO': "[A-Z]{4}\\d{20}",
+ 'EE': "\\d{16}",
+ 'FO': "\\d{14}",
+ 'FI': "\\d{14}",
+ 'FR': "\\d{10}[\\dA-Z]{11}\\d{2}",
+ 'GE': "[\\dA-Z]{2}\\d{16}",
+ 'DE': "\\d{18}",
+ 'GI': "[A-Z]{4}[\\dA-Z]{15}",
+ 'GR': "\\d{7}[\\dA-Z]{16}",
+ 'GL': "\\d{14}",
+ 'GT': "[\\dA-Z]{4}[\\dA-Z]{20}",
+ 'HU': "\\d{24}",
+ 'IS': "\\d{22}",
+ 'IE': "[\\dA-Z]{4}\\d{14}",
+ 'IL': "\\d{19}",
+ 'IT': "[A-Z]\\d{10}[\\dA-Z]{12}",
+ 'KZ': "\\d{3}[\\dA-Z]{13}",
+ 'KW': "[A-Z]{4}[\\dA-Z]{22}",
+ 'LV': "[A-Z]{4}[\\dA-Z]{13}",
+ 'LB': "\\d{4}[\\dA-Z]{20}",
+ 'LI': "\\d{5}[\\dA-Z]{12}",
+ 'LT': "\\d{16}",
+ 'LU': "\\d{3}[\\dA-Z]{13}",
+ 'MK': "\\d{3}[\\dA-Z]{10}\\d{2}",
+ 'MT': "[A-Z]{4}\\d{5}[\\dA-Z]{18}",
+ 'MR': "\\d{23}",
+ 'MU': "[A-Z]{4}\\d{19}[A-Z]{3}",
+ 'MC': "\\d{10}[\\dA-Z]{11}\\d{2}",
+ 'MD': "[\\dA-Z]{2}\\d{18}",
+ 'ME': "\\d{18}",
+ 'NL': "[A-Z]{4}\\d{10}",
+ 'NO': "\\d{11}",
+ 'PK': "[\\dA-Z]{4}\\d{16}",
+ 'PS': "[\\dA-Z]{4}\\d{21}",
+ 'PL': "\\d{24}",
+ 'PT': "\\d{21}",
+ 'RO': "[A-Z]{4}[\\dA-Z]{16}",
+ 'SM': "[A-Z]\\d{10}[\\dA-Z]{12}",
+ 'SA': "\\d{2}[\\dA-Z]{18}",
+ 'RS': "\\d{18}",
+ 'SK': "\\d{20}",
+ 'SI': "\\d{15}",
+ 'ES': "\\d{20}",
+ 'SE': "\\d{20}",
+ 'CH': "\\d{5}[\\dA-Z]{12}",
+ 'TN': "\\d{20}",
+ 'TR': "\\d{5}[\\dA-Z]{17}",
+ 'AE': "\\d{3}\\d{16}",
+ 'GB': "[A-Z]{4}\\d{14}",
+ 'VG': "[\\dA-Z]{4}\\d{16}"
+ };
+ var bbanpattern = bbancountrypatterns[countrycode];
+ // As new countries will start using IBAN in the
+ // future, we only check if the countrycode is known.
+ // This prevents false negatives, while almost all
+ // false positives introduced by this, will be caught
+ // by the checksum validation below anyway.
+ // Strict checking should return FALSE for unknown
+ // countries.
+ if (typeof bbanpattern !== 'undefined') {
+ var ibanregexp = new RegExp("^[A-Z]{2}\\d{2}" + bbanpattern + "$", "");
+ if (!(ibanregexp.test(iban))) {
+ return false; // invalid country specific format
+ }
+ }
+
+ // now check the checksum, first convert to digits
+ var ibancheck = iban.substring(4,iban.length) + iban.substring(0,4);
+ var ibancheckdigits = "";
+ var leadingZeroes = true;
+ var charAt;
+ for (var i =0; i 9 &&
+ phone_number.match(/^(\+?1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
+}, "Please specify a valid phone number");
+
+jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
+ phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
+ return this.optional(element) || phone_number.length > 9 &&
+ phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?)|(?:\(?0))(?:\d{2}\)?\s?\d{4}\s?\d{4}|\d{3}\)?\s?\d{3}\s?\d{3,4}|\d{4}\)?\s?(?:\d{5}|\d{3}\s?\d{3})|\d{5}\)?\s?\d{4,5})$/);
+}, 'Please specify a valid phone number');
+
+jQuery.validator.addMethod('mobileUK', function(phone_number, element) {
+ phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
+ return this.optional(element) || phone_number.length > 9 &&
+ phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)7(?:[45789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
+}, 'Please specify a valid mobile number');
+
+//Matches UK landline + mobile, accepting only 01-3 for landline or 07 for mobile to exclude many premium numbers
+jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
+ phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
+ return this.optional(element) || phone_number.length > 9 &&
+ phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[45789]\d{8}|624\d{6})))$/);
+}, 'Please specify a valid uk phone number');
+// On the above three UK functions, do the following server side processing:
+// Compare original input with this RegEx pattern:
+// ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$
+// Extract $1 and set $prefix to '+44' if $1 is '44', otherwise set $prefix to '0'
+// Extract $2 and remove hyphens, spaces and parentheses. Phone number is combined $prefix and $2.
+// A number of very detailed GB telephone number RegEx patterns can also be found at:
+// http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
+
+// Matches UK postcode. Does not match to UK Channel Islands that have their own postcodes (non standard UK)
+jQuery.validator.addMethod('postcodeUK', function(value, element) {
+ return this.optional(element) || /^((([A-PR-UWYZ][0-9])|([A-PR-UWYZ][0-9][0-9])|([A-PR-UWYZ][A-HK-Y][0-9])|([A-PR-UWYZ][A-HK-Y][0-9][0-9])|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\s?(0AA))$/i.test(value);
+}, 'Please specify a valid UK postcode');
+
+// TODO check if value starts with <, otherwise don't try stripping anything
+jQuery.validator.addMethod("strippedminlength", function(value, element, param) {
+ return jQuery(value).text().length >= param;
+}, jQuery.validator.format("Please enter at least {0} characters"));
+
+// same as email, but TLD is optional
+jQuery.validator.addMethod("email2", function(value, element, param) {
+ return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
+}, jQuery.validator.messages.email);
+
+// same as url, but TLD is optional
+jQuery.validator.addMethod("url2", function(value, element, param) {
+ return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
+}, jQuery.validator.messages.url);
+
+// NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator
+// Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0
+// Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings)
+jQuery.validator.addMethod("creditcardtypes", function(value, element, param) {
+ if (/[^0-9\-]+/.test(value)) {
+ return false;
+ }
+
+ value = value.replace(/\D/g, "");
+
+ var validTypes = 0x0000;
+
+ if (param.mastercard) {
+ validTypes |= 0x0001;
+ }
+ if (param.visa) {
+ validTypes |= 0x0002;
+ }
+ if (param.amex) {
+ validTypes |= 0x0004;
+ }
+ if (param.dinersclub) {
+ validTypes |= 0x0008;
+ }
+ if (param.enroute) {
+ validTypes |= 0x0010;
+ }
+ if (param.discover) {
+ validTypes |= 0x0020;
+ }
+ if (param.jcb) {
+ validTypes |= 0x0040;
+ }
+ if (param.unknown) {
+ validTypes |= 0x0080;
+ }
+ if (param.all) {
+ validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
+ }
+ if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard
+ return value.length === 16;
+ }
+ if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
+ return value.length === 16;
+ }
+ if (validTypes & 0x0004 && /^(3[47])/.test(value)) { //amex
+ return value.length === 15;
+ }
+ if (validTypes & 0x0008 && /^(3(0[012345]|[68]))/.test(value)) { //dinersclub
+ return value.length === 14;
+ }
+ if (validTypes & 0x0010 && /^(2(014|149))/.test(value)) { //enroute
+ return value.length === 15;
+ }
+ if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover
+ return value.length === 16;
+ }
+ if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb
+ return value.length === 16;
+ }
+ if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb
+ return value.length === 15;
+ }
+ if (validTypes & 0x0080) { //unknown
+ return true;
+ }
+ return false;
+}, "Please enter a valid credit card number.");
+
+jQuery.validator.addMethod("ipv4", function(value, element, param) {
+ return this.optional(element) || /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/i.test(value);
+}, "Please enter a valid IP v4 address.");
+
+jQuery.validator.addMethod("ipv6", function(value, element, param) {
+ return this.optional(element) || /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test(value);
+}, "Please enter a valid IP v6 address.");
+
+/**
+* Return true if the field value matches the given format RegExp
+*
+* @example jQuery.validator.methods.pattern("AR1004",element,/^AR\d{4}$/)
+* @result true
+*
+* @example jQuery.validator.methods.pattern("BR1004",element,/^AR\d{4}$/)
+* @result false
+*
+* @name jQuery.validator.methods.pattern
+* @type Boolean
+* @cat Plugins/Validate/Methods
+*/
+jQuery.validator.addMethod("pattern", function(value, element, param) {
+ if (this.optional(element)) {
+ return true;
+ }
+ if (typeof param === 'string') {
+ param = new RegExp('^(?:' + param + ')$');
+ }
+ return param.test(value);
+}, "Invalid format.");
+
+
+/*
+ * Lets you say "at least X inputs that match selector Y must be filled."
+ *
+ * The end result is that neither of these inputs:
+ *
+ *
+ *
+ *
+ * ...will validate unless at least one of them is filled.
+ *
+ * partnumber: {require_from_group: [1,".productinfo"]},
+ * description: {require_from_group: [1,".productinfo"]}
+ *
+ */
+jQuery.validator.addMethod("require_from_group", function(value, element, options) {
+ var validator = this;
+ var selector = options[1];
+ var validOrNot = $(selector, element.form).filter(function() {
+ return validator.elementValue(this);
+ }).length >= options[0];
+
+ if(!$(element).data('being_validated')) {
+ var fields = $(selector, element.form);
+ fields.data('being_validated', true);
+ fields.valid();
+ fields.data('being_validated', false);
+ }
+ return validOrNot;
+}, jQuery.format("Please fill at least {0} of these fields."));
+
+/*
+ * Lets you say "either at least X inputs that match selector Y must be filled,
+ * OR they must all be skipped (left blank)."
+ *
+ * The end result, is that none of these inputs:
+ *
+ *
+ *
+ *
+ *
+ * ...will validate unless either at least two of them are filled,
+ * OR none of them are.
+ *
+ * partnumber: {skip_or_fill_minimum: [2,".productinfo"]},
+ * description: {skip_or_fill_minimum: [2,".productinfo"]},
+ * color: {skip_or_fill_minimum: [2,".productinfo"]}
+ *
+ */
+jQuery.validator.addMethod("skip_or_fill_minimum", function(value, element, options) {
+ var validator = this,
+ numberRequired = options[0],
+ selector = options[1];
+ var numberFilled = $(selector, element.form).filter(function() {
+ return validator.elementValue(this);
+ }).length;
+ var valid = numberFilled >= numberRequired || numberFilled === 0;
+
+ if(!$(element).data('being_validated')) {
+ var fields = $(selector, element.form);
+ fields.data('being_validated', true);
+ fields.valid();
+ fields.data('being_validated', false);
+ }
+ return valid;
+}, jQuery.format("Please either skip these fields or fill at least {0} of them."));
+
+// Accept a value from a file input based on a required mimetype
+jQuery.validator.addMethod("accept", function(value, element, param) {
+ // Split mime on commas in case we have multiple types we can accept
+ var typeParam = typeof param === "string" ? param.replace(/\s/g, '').replace(/,/g, '|') : "image/*",
+ optionalValue = this.optional(element),
+ i, file;
+
+ // Element is optional
+ if (optionalValue) {
+ return optionalValue;
+ }
+
+ if ($(element).attr("type") === "file") {
+ // If we are using a wildcard, make it regex friendly
+ typeParam = typeParam.replace(/\*/g, ".*");
+
+ // Check if the element has a FileList before checking each file
+ if (element.files && element.files.length) {
+ for (i = 0; i < element.files.length; i++) {
+ file = element.files[i];
+
+ // Grab the mimetype from the loaded file, verify it matches
+ if (!file.type.match(new RegExp( ".?(" + typeParam + ")$", "i"))) {
+ return false;
+ }
+ }
+ }
+ }
+
+ // Either return true because we've validated each file, or because the
+ // browser does not support element.files and the FileList feature
+ return true;
+}, jQuery.format("Please enter a value with a valid mimetype."));
+
+// Older "accept" file extension method. Old docs: http://docs.jquery.com/Plugins/Validation/Methods/accept
+jQuery.validator.addMethod("extension", function(value, element, param) {
+ param = typeof param === "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif";
+ return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));
+}, jQuery.format("Please enter a value with a valid extension."));
diff --git a/lib/assets/javascripts/surveyor/jquery.validate.js b/lib/assets/javascripts/surveyor/jquery.validate.js
new file mode 100755
index 00000000..88ba300e
--- /dev/null
+++ b/lib/assets/javascripts/surveyor/jquery.validate.js
@@ -0,0 +1,1231 @@
+/*!
+ * jQuery Validation Plugin 1.11.1
+ *
+ * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
+ * http://docs.jquery.com/Plugins/Validation
+ *
+ * Copyright 2013 Jörn Zaefferer
+ * Released under the MIT license:
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+(function($) {
+
+$.extend($.fn, {
+ // http://docs.jquery.com/Plugins/Validation/validate
+ validate: function( options ) {
+
+ // if nothing is selected, return nothing; can't chain anyway
+ if ( !this.length ) {
+ if ( options && options.debug && window.console ) {
+ console.warn( "Nothing selected, can't validate, returning nothing." );
+ }
+ return;
+ }
+
+ // check if a validator for this form was already created
+ var validator = $.data( this[0], "validator" );
+ if ( validator ) {
+ return validator;
+ }
+
+ // Add novalidate tag if HTML5.
+ this.attr( "novalidate", "novalidate" );
+
+ validator = new $.validator( options, this[0] );
+ $.data( this[0], "validator", validator );
+
+ if ( validator.settings.onsubmit ) {
+
+ this.validateDelegate( ":submit", "click", function( event ) {
+ if ( validator.settings.submitHandler ) {
+ validator.submitButton = event.target;
+ }
+ // allow suppressing validation by adding a cancel class to the submit button
+ if ( $(event.target).hasClass("cancel") ) {
+ validator.cancelSubmit = true;
+ }
+
+ // allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
+ if ( $(event.target).attr("formnovalidate") !== undefined ) {
+ validator.cancelSubmit = true;
+ }
+ });
+
+ // validate the form on submit
+ this.submit( function( event ) {
+ if ( validator.settings.debug ) {
+ // prevent form submit to be able to see console output
+ event.preventDefault();
+ }
+ function handle() {
+ var hidden;
+ if ( validator.settings.submitHandler ) {
+ if ( validator.submitButton ) {
+ // insert a hidden input as a replacement for the missing submit button
+ hidden = $("").attr("name", validator.submitButton.name).val( $(validator.submitButton).val() ).appendTo(validator.currentForm);
+ }
+ validator.settings.submitHandler.call( validator, validator.currentForm, event );
+ if ( validator.submitButton ) {
+ // and clean up afterwards; thanks to no-block-scope, hidden can be referenced
+ hidden.remove();
+ }
+ return false;
+ }
+ return true;
+ }
+
+ // prevent submit for invalid forms or custom submit handlers
+ if ( validator.cancelSubmit ) {
+ validator.cancelSubmit = false;
+ return handle();
+ }
+ if ( validator.form() ) {
+ if ( validator.pendingRequest ) {
+ validator.formSubmitted = true;
+ return false;
+ }
+ return handle();
+ } else {
+ validator.focusInvalid();
+ return false;
+ }
+ });
+ }
+
+ return validator;
+ },
+ // http://docs.jquery.com/Plugins/Validation/valid
+ valid: function() {
+ if ( $(this[0]).is("form")) {
+ return this.validate().form();
+ } else {
+ var valid = true;
+ var validator = $(this[0].form).validate();
+ this.each(function() {
+ valid = valid && validator.element(this);
+ });
+ return valid;
+ }
+ },
+ // attributes: space seperated list of attributes to retrieve and remove
+ removeAttrs: function( attributes ) {
+ var result = {},
+ $element = this;
+ $.each(attributes.split(/\s/), function( index, value ) {
+ result[value] = $element.attr(value);
+ $element.removeAttr(value);
+ });
+ return result;
+ },
+ // http://docs.jquery.com/Plugins/Validation/rules
+ rules: function( command, argument ) {
+ var element = this[0];
+
+ if ( command ) {
+ var settings = $.data(element.form, "validator").settings;
+ var staticRules = settings.rules;
+ var existingRules = $.validator.staticRules(element);
+ switch(command) {
+ case "add":
+ $.extend(existingRules, $.validator.normalizeRule(argument));
+ // remove messages from rules, but allow them to be set separetely
+ delete existingRules.messages;
+ staticRules[element.name] = existingRules;
+ if ( argument.messages ) {
+ settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
+ }
+ break;
+ case "remove":
+ if ( !argument ) {
+ delete staticRules[element.name];
+ return existingRules;
+ }
+ var filtered = {};
+ $.each(argument.split(/\s/), function( index, method ) {
+ filtered[method] = existingRules[method];
+ delete existingRules[method];
+ });
+ return filtered;
+ }
+ }
+
+ var data = $.validator.normalizeRules(
+ $.extend(
+ {},
+ $.validator.classRules(element),
+ $.validator.attributeRules(element),
+ $.validator.dataRules(element),
+ $.validator.staticRules(element)
+ ), element);
+
+ // make sure required is at front
+ if ( data.required ) {
+ var param = data.required;
+ delete data.required;
+ data = $.extend({required: param}, data);
+ }
+
+ return data;
+ }
+});
+
+// Custom selectors
+$.extend($.expr[":"], {
+ // http://docs.jquery.com/Plugins/Validation/blank
+ blank: function( a ) { return !$.trim("" + $(a).val()); },
+ // http://docs.jquery.com/Plugins/Validation/filled
+ filled: function( a ) { return !!$.trim("" + $(a).val()); },
+ // http://docs.jquery.com/Plugins/Validation/unchecked
+ unchecked: function( a ) { return !$(a).prop("checked"); }
+});
+
+// constructor for validator
+$.validator = function( options, form ) {
+ this.settings = $.extend( true, {}, $.validator.defaults, options );
+ this.currentForm = form;
+ this.init();
+};
+
+$.validator.format = function( source, params ) {
+ if ( arguments.length === 1 ) {
+ return function() {
+ var args = $.makeArray(arguments);
+ args.unshift(source);
+ return $.validator.format.apply( this, args );
+ };
+ }
+ if ( arguments.length > 2 && params.constructor !== Array ) {
+ params = $.makeArray(arguments).slice(1);
+ }
+ if ( params.constructor !== Array ) {
+ params = [ params ];
+ }
+ $.each(params, function( i, n ) {
+ source = source.replace( new RegExp("\\{" + i + "\\}", "g"), function() {
+ return n;
+ });
+ });
+ return source;
+};
+
+$.extend($.validator, {
+
+ defaults: {
+ messages: {},
+ groups: {},
+ rules: {},
+ errorClass: "error",
+ validClass: "valid",
+ errorElement: "label",
+ focusInvalid: true,
+ errorContainer: $([]),
+ errorLabelContainer: $([]),
+ onsubmit: true,
+ ignore: ":hidden",
+ ignoreTitle: false,
+ onfocusin: function( element, event ) {
+ this.lastActive = element;
+
+ // hide error label and remove error class on focus if enabled
+ if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
+ if ( this.settings.unhighlight ) {
+ this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
+ }
+ this.addWrapper(this.errorsFor(element)).hide();
+ }
+ },
+ onfocusout: function( element, event ) {
+ if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) {
+ this.element(element);
+ }
+ },
+ onkeyup: function( element, event ) {
+ if ( event.which === 9 && this.elementValue(element) === "" ) {
+ return;
+ } else if ( element.name in this.submitted || element === this.lastElement ) {
+ this.element(element);
+ }
+ },
+ onclick: function( element, event ) {
+ // click on selects, radiobuttons and checkboxes
+ if ( element.name in this.submitted ) {
+ this.element(element);
+ }
+ // or option elements, check parent select in that case
+ else if ( element.parentNode.name in this.submitted ) {
+ this.element(element.parentNode);
+ }
+ },
+ highlight: function( element, errorClass, validClass ) {
+ if ( element.type === "radio" ) {
+ this.findByName(element.name).addClass(errorClass).removeClass(validClass);
+ } else {
+ $(element).addClass(errorClass).removeClass(validClass);
+ }
+ },
+ unhighlight: function( element, errorClass, validClass ) {
+ if ( element.type === "radio" ) {
+ this.findByName(element.name).removeClass(errorClass).addClass(validClass);
+ } else {
+ $(element).removeClass(errorClass).addClass(validClass);
+ }
+ }
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults
+ setDefaults: function( settings ) {
+ $.extend( $.validator.defaults, settings );
+ },
+
+ messages: {
+ required: "This field is required.",
+ remote: "Please fix this field.",
+ email: "Please enter a valid email address.",
+ url: "Please enter a valid URL.",
+ date: "Please enter a valid date.",
+ dateISO: "Please enter a valid date (ISO).",
+ number: "Please enter a valid number.",
+ digits: "Please enter only digits.",
+ creditcard: "Please enter a valid credit card number.",
+ equalTo: "Please enter the same value again.",
+ maxlength: $.validator.format("Please enter no more than {0} characters."),
+ minlength: $.validator.format("Please enter at least {0} characters."),
+ rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
+ range: $.validator.format("Please enter a value between {0} and {1}."),
+ max: $.validator.format("Please enter a value less than or equal to {0}."),
+ min: $.validator.format("Please enter a value greater than or equal to {0}.")
+ },
+
+ autoCreateRanges: false,
+
+ prototype: {
+
+ init: function() {
+ this.labelContainer = $(this.settings.errorLabelContainer);
+ this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
+ this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
+ this.submitted = {};
+ this.valueCache = {};
+ this.pendingRequest = 0;
+ this.pending = {};
+ this.invalid = {};
+ this.reset();
+
+ var groups = (this.groups = {});
+ $.each(this.settings.groups, function( key, value ) {
+ if ( typeof value === "string" ) {
+ value = value.split(/\s/);
+ }
+ $.each(value, function( index, name ) {
+ groups[name] = key;
+ });
+ });
+ var rules = this.settings.rules;
+ $.each(rules, function( key, value ) {
+ rules[key] = $.validator.normalizeRule(value);
+ });
+
+ function delegate(event) {
+ var validator = $.data(this[0].form, "validator"),
+ eventType = "on" + event.type.replace(/^validate/, "");
+ if ( validator.settings[eventType] ) {
+ validator.settings[eventType].call(validator, this[0], event);
+ }
+ }
+ $(this.currentForm)
+ .validateDelegate(":text, [type='password'], [type='file'], select, textarea, " +
+ "[type='number'], [type='search'] ,[type='tel'], [type='url'], " +
+ "[type='email'], [type='datetime'], [type='date'], [type='month'], " +
+ "[type='week'], [type='time'], [type='datetime-local'], " +
+ "[type='range'], [type='color'] ",
+ "focusin focusout keyup", delegate)
+ .validateDelegate("[type='radio'], [type='checkbox'], select, option", "click", delegate);
+
+ if ( this.settings.invalidHandler ) {
+ $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
+ }
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/form
+ form: function() {
+ this.checkForm();
+ $.extend(this.submitted, this.errorMap);
+ this.invalid = $.extend({}, this.errorMap);
+ if ( !this.valid() ) {
+ $(this.currentForm).triggerHandler("invalid-form", [this]);
+ }
+ this.showErrors();
+ return this.valid();
+ },
+
+ checkForm: function() {
+ this.prepareForm();
+ for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
+ this.check( elements[i] );
+ }
+ return this.valid();
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/element
+ element: function( element ) {
+ element = this.validationTargetFor( this.clean( element ) );
+ this.lastElement = element;
+ this.prepareElement( element );
+ this.currentElements = $(element);
+ var result = this.check( element ) !== false;
+ if ( result ) {
+ delete this.invalid[element.name];
+ } else {
+ this.invalid[element.name] = true;
+ }
+ if ( !this.numberOfInvalids() ) {
+ // Hide error containers on last error
+ this.toHide = this.toHide.add( this.containers );
+ }
+ this.showErrors();
+ return result;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/showErrors
+ showErrors: function( errors ) {
+ if ( errors ) {
+ // add items to error list and map
+ $.extend( this.errorMap, errors );
+ this.errorList = [];
+ for ( var name in errors ) {
+ this.errorList.push({
+ message: errors[name],
+ element: this.findByName(name)[0]
+ });
+ }
+ // remove items from success list
+ this.successList = $.grep( this.successList, function( element ) {
+ return !(element.name in errors);
+ });
+ }
+ if ( this.settings.showErrors ) {
+ this.settings.showErrors.call( this, this.errorMap, this.errorList );
+ } else {
+ this.defaultShowErrors();
+ }
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
+ resetForm: function() {
+ if ( $.fn.resetForm ) {
+ $(this.currentForm).resetForm();
+ }
+ this.submitted = {};
+ this.lastElement = null;
+ this.prepareForm();
+ this.hideErrors();
+ this.elements().removeClass( this.settings.errorClass ).removeData( "previousValue" );
+ },
+
+ numberOfInvalids: function() {
+ return this.objectLength(this.invalid);
+ },
+
+ objectLength: function( obj ) {
+ var count = 0;
+ for ( var i in obj ) {
+ count++;
+ }
+ return count;
+ },
+
+ hideErrors: function() {
+ this.addWrapper( this.toHide ).hide();
+ },
+
+ valid: function() {
+ return this.size() === 0;
+ },
+
+ size: function() {
+ return this.errorList.length;
+ },
+
+ focusInvalid: function() {
+ if ( this.settings.focusInvalid ) {
+ try {
+ $(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
+ .filter(":visible")
+ .focus()
+ // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
+ .trigger("focusin");
+ } catch(e) {
+ // ignore IE throwing errors when focusing hidden elements
+ }
+ }
+ },
+
+ findLastActive: function() {
+ var lastActive = this.lastActive;
+ return lastActive && $.grep(this.errorList, function( n ) {
+ return n.element.name === lastActive.name;
+ }).length === 1 && lastActive;
+ },
+
+ elements: function() {
+ var validator = this,
+ rulesCache = {};
+
+ // select all valid inputs inside the form (no submit or reset buttons)
+ return $(this.currentForm)
+ .find("input, select, textarea")
+ .not(":submit, :reset, :image, [disabled]")
+ .not( this.settings.ignore )
+ .filter(function() {
+ if ( !this.name && validator.settings.debug && window.console ) {
+ console.error( "%o has no name assigned", this);
+ }
+
+ // select only the first element for each name, and only those with rules specified
+ if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
+ return false;
+ }
+
+ rulesCache[this.name] = true;
+ return true;
+ });
+ },
+
+ clean: function( selector ) {
+ return $(selector)[0];
+ },
+
+ errors: function() {
+ var errorClass = this.settings.errorClass.replace(" ", ".");
+ return $(this.settings.errorElement + "." + errorClass, this.errorContext);
+ },
+
+ reset: function() {
+ this.successList = [];
+ this.errorList = [];
+ this.errorMap = {};
+ this.toShow = $([]);
+ this.toHide = $([]);
+ this.currentElements = $([]);
+ },
+
+ prepareForm: function() {
+ this.reset();
+ this.toHide = this.errors().add( this.containers );
+ },
+
+ prepareElement: function( element ) {
+ this.reset();
+ this.toHide = this.errorsFor(element);
+ },
+
+ elementValue: function( element ) {
+ var type = $(element).attr("type"),
+ val = $(element).val();
+
+ if ( type === "radio" || type === "checkbox" ) {
+ return $("input[name='" + $(element).attr("name") + "']:checked").val();
+ }
+
+ if ( typeof val === "string" ) {
+ return val.replace(/\r/g, "");
+ }
+ return val;
+ },
+
+ check: function( element ) {
+ element = this.validationTargetFor( this.clean( element ) );
+
+ var rules = $(element).rules();
+ var dependencyMismatch = false;
+ var val = this.elementValue(element);
+ var result;
+
+ for (var method in rules ) {
+ var rule = { method: method, parameters: rules[method] };
+ try {
+
+ result = $.validator.methods[method].call( this, val, element, rule.parameters );
+
+ // if a method indicates that the field is optional and therefore valid,
+ // don't mark it as valid when there are no other rules
+ if ( result === "dependency-mismatch" ) {
+ dependencyMismatch = true;
+ continue;
+ }
+ dependencyMismatch = false;
+
+ if ( result === "pending" ) {
+ this.toHide = this.toHide.not( this.errorsFor(element) );
+ return;
+ }
+
+ if ( !result ) {
+ this.formatAndAdd( element, rule );
+ return false;
+ }
+ } catch(e) {
+ if ( this.settings.debug && window.console ) {
+ console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
+ }
+ throw e;
+ }
+ }
+ if ( dependencyMismatch ) {
+ return;
+ }
+ if ( this.objectLength(rules) ) {
+ this.successList.push(element);
+ }
+ return true;
+ },
+
+ // return the custom message for the given element and validation method
+ // specified in the element's HTML5 data attribute
+ customDataMessage: function( element, method ) {
+ return $(element).data("msg-" + method.toLowerCase()) || (element.attributes && $(element).attr("data-msg-" + method.toLowerCase()));
+ },
+
+ // return the custom message for the given element name and validation method
+ customMessage: function( name, method ) {
+ var m = this.settings.messages[name];
+ return m && (m.constructor === String ? m : m[method]);
+ },
+
+ // return the first defined argument, allowing empty strings
+ findDefined: function() {
+ for(var i = 0; i < arguments.length; i++) {
+ if ( arguments[i] !== undefined ) {
+ return arguments[i];
+ }
+ }
+ return undefined;
+ },
+
+ defaultMessage: function( element, method ) {
+ return this.findDefined(
+ this.customMessage( element.name, method ),
+ this.customDataMessage( element, method ),
+ // title is never undefined, so handle empty string as undefined
+ !this.settings.ignoreTitle && element.title || undefined,
+ $.validator.messages[method],
+ "Warning: No message defined for " + element.name + ""
+ );
+ },
+
+ formatAndAdd: function( element, rule ) {
+ var message = this.defaultMessage( element, rule.method ),
+ theregex = /\$?\{(\d+)\}/g;
+ if ( typeof message === "function" ) {
+ message = message.call(this, rule.parameters, element);
+ } else if (theregex.test(message)) {
+ message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
+ }
+ this.errorList.push({
+ message: message,
+ element: element
+ });
+
+ this.errorMap[element.name] = message;
+ this.submitted[element.name] = message;
+ },
+
+ addWrapper: function( toToggle ) {
+ if ( this.settings.wrapper ) {
+ toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
+ }
+ return toToggle;
+ },
+
+ defaultShowErrors: function() {
+ var i, elements;
+ for ( i = 0; this.errorList[i]; i++ ) {
+ var error = this.errorList[i];
+ if ( this.settings.highlight ) {
+ this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
+ }
+ this.showLabel( error.element, error.message );
+ }
+ if ( this.errorList.length ) {
+ this.toShow = this.toShow.add( this.containers );
+ }
+ if ( this.settings.success ) {
+ for ( i = 0; this.successList[i]; i++ ) {
+ this.showLabel( this.successList[i] );
+ }
+ }
+ if ( this.settings.unhighlight ) {
+ for ( i = 0, elements = this.validElements(); elements[i]; i++ ) {
+ this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
+ }
+ }
+ this.toHide = this.toHide.not( this.toShow );
+ this.hideErrors();
+ this.addWrapper( this.toShow ).show();
+ },
+
+ validElements: function() {
+ return this.currentElements.not(this.invalidElements());
+ },
+
+ invalidElements: function() {
+ return $(this.errorList).map(function() {
+ return this.element;
+ });
+ },
+
+ showLabel: function( element, message ) {
+ var label = this.errorsFor( element );
+ if ( label.length ) {
+ // refresh error/success class
+ label.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
+ // replace message on existing label
+ label.html(message);
+ } else {
+ // create label
+ label = $("<" + this.settings.errorElement + ">")
+ .attr("for", this.idOrName(element))
+ .addClass(this.settings.errorClass)
+ .html(message || "");
+ if ( this.settings.wrapper ) {
+ // make sure the element is visible, even in IE
+ // actually showing the wrapped element is handled elsewhere
+ label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
+ }
+ if ( !this.labelContainer.append(label).length ) {
+ if ( this.settings.errorPlacement ) {
+ this.settings.errorPlacement(label, $(element) );
+ } else {
+ label.insertAfter(element);
+ }
+ }
+ }
+ if ( !message && this.settings.success ) {
+ label.text("");
+ if ( typeof this.settings.success === "string" ) {
+ label.addClass( this.settings.success );
+ } else {
+ this.settings.success( label, element );
+ }
+ }
+ this.toShow = this.toShow.add(label);
+ },
+
+ errorsFor: function( element ) {
+ var name = this.idOrName(element);
+ return this.errors().filter(function() {
+ return $(this).attr("for") === name;
+ });
+ },
+
+ idOrName: function( element ) {
+ return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
+ },
+
+ validationTargetFor: function( element ) {
+ // if radio/checkbox, validate first element in group instead
+ if ( this.checkable(element) ) {
+ element = this.findByName( element.name ).not(this.settings.ignore)[0];
+ }
+ return element;
+ },
+
+ checkable: function( element ) {
+ return (/radio|checkbox/i).test(element.type);
+ },
+
+ findByName: function( name ) {
+ return $(this.currentForm).find("[name='" + name + "']");
+ },
+
+ getLength: function( value, element ) {
+ switch( element.nodeName.toLowerCase() ) {
+ case "select":
+ return $("option:selected", element).length;
+ case "input":
+ if ( this.checkable( element) ) {
+ return this.findByName(element.name).filter(":checked").length;
+ }
+ }
+ return value.length;
+ },
+
+ depend: function( param, element ) {
+ return this.dependTypes[typeof param] ? this.dependTypes[typeof param](param, element) : true;
+ },
+
+ dependTypes: {
+ "boolean": function( param, element ) {
+ return param;
+ },
+ "string": function( param, element ) {
+ return !!$(param, element.form).length;
+ },
+ "function": function( param, element ) {
+ return param(element);
+ }
+ },
+
+ optional: function( element ) {
+ var val = this.elementValue(element);
+ return !$.validator.methods.required.call(this, val, element) && "dependency-mismatch";
+ },
+
+ startRequest: function( element ) {
+ if ( !this.pending[element.name] ) {
+ this.pendingRequest++;
+ this.pending[element.name] = true;
+ }
+ },
+
+ stopRequest: function( element, valid ) {
+ this.pendingRequest--;
+ // sometimes synchronization fails, make sure pendingRequest is never < 0
+ if ( this.pendingRequest < 0 ) {
+ this.pendingRequest = 0;
+ }
+ delete this.pending[element.name];
+ if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
+ $(this.currentForm).submit();
+ this.formSubmitted = false;
+ } else if (!valid && this.pendingRequest === 0 && this.formSubmitted) {
+ $(this.currentForm).triggerHandler("invalid-form", [this]);
+ this.formSubmitted = false;
+ }
+ },
+
+ previousValue: function( element ) {
+ return $.data(element, "previousValue") || $.data(element, "previousValue", {
+ old: null,
+ valid: true,
+ message: this.defaultMessage( element, "remote" )
+ });
+ }
+
+ },
+
+ classRuleSettings: {
+ required: {required: true},
+ email: {email: true},
+ url: {url: true},
+ date: {date: true},
+ dateISO: {dateISO: true},
+ number: {number: true},
+ digits: {digits: true},
+ creditcard: {creditcard: true}
+ },
+
+ addClassRules: function( className, rules ) {
+ if ( className.constructor === String ) {
+ this.classRuleSettings[className] = rules;
+ } else {
+ $.extend(this.classRuleSettings, className);
+ }
+ },
+
+ classRules: function( element ) {
+ var rules = {};
+ var classes = $(element).attr("class");
+ if ( classes ) {
+ $.each(classes.split(" "), function() {
+ if ( this in $.validator.classRuleSettings ) {
+ $.extend(rules, $.validator.classRuleSettings[this]);
+ }
+ });
+ }
+ return rules;
+ },
+
+ attributeRules: function( element ) {
+ var rules = {};
+ var $element = $(element);
+ var type = $element[0].getAttribute("type");
+
+ for (var method in $.validator.methods) {
+ var value;
+
+ // support for in both html5 and older browsers
+ if ( method === "required" ) {
+ value = $element.get(0).getAttribute(method);
+ // Some browsers return an empty string for the required attribute
+ // and non-HTML5 browsers might have required="" markup
+ if ( value === "" ) {
+ value = true;
+ }
+ // force non-HTML5 browsers to return bool
+ value = !!value;
+ } else {
+ value = $element.attr(method);
+ }
+
+ // convert the value to a number for number inputs, and for text for backwards compability
+ // allows type="date" and others to be compared as strings
+ if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
+ value = Number(value);
+ }
+
+ if ( value ) {
+ rules[method] = value;
+ } else if ( type === method && type !== 'range' ) {
+ // exception: the jquery validate 'range' method
+ // does not test for the html5 'range' type
+ rules[method] = true;
+ }
+ }
+
+ // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
+ if ( rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength) ) {
+ delete rules.maxlength;
+ }
+
+ return rules;
+ },
+
+ dataRules: function( element ) {
+ var method, value,
+ rules = {}, $element = $(element);
+ for (method in $.validator.methods) {
+ value = $element.data("rule-" + method.toLowerCase());
+ if ( value !== undefined ) {
+ rules[method] = value;
+ }
+ }
+ return rules;
+ },
+
+ staticRules: function( element ) {
+ var rules = {};
+ var validator = $.data(element.form, "validator");
+ if ( validator.settings.rules ) {
+ rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
+ }
+ return rules;
+ },
+
+ normalizeRules: function( rules, element ) {
+ // handle dependency check
+ $.each(rules, function( prop, val ) {
+ // ignore rule when param is explicitly false, eg. required:false
+ if ( val === false ) {
+ delete rules[prop];
+ return;
+ }
+ if ( val.param || val.depends ) {
+ var keepRule = true;
+ switch (typeof val.depends) {
+ case "string":
+ keepRule = !!$(val.depends, element.form).length;
+ break;
+ case "function":
+ keepRule = val.depends.call(element, element);
+ break;
+ }
+ if ( keepRule ) {
+ rules[prop] = val.param !== undefined ? val.param : true;
+ } else {
+ delete rules[prop];
+ }
+ }
+ });
+
+ // evaluate parameters
+ $.each(rules, function( rule, parameter ) {
+ rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter;
+ });
+
+ // clean number parameters
+ $.each(['minlength', 'maxlength'], function() {
+ if ( rules[this] ) {
+ rules[this] = Number(rules[this]);
+ }
+ });
+ $.each(['rangelength', 'range'], function() {
+ var parts;
+ if ( rules[this] ) {
+ if ( $.isArray(rules[this]) ) {
+ rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
+ } else if ( typeof rules[this] === "string" ) {
+ parts = rules[this].split(/[\s,]+/);
+ rules[this] = [Number(parts[0]), Number(parts[1])];
+ }
+ }
+ });
+
+ if ( $.validator.autoCreateRanges ) {
+ // auto-create ranges
+ if ( rules.min && rules.max ) {
+ rules.range = [rules.min, rules.max];
+ delete rules.min;
+ delete rules.max;
+ }
+ if ( rules.minlength && rules.maxlength ) {
+ rules.rangelength = [rules.minlength, rules.maxlength];
+ delete rules.minlength;
+ delete rules.maxlength;
+ }
+ }
+
+ return rules;
+ },
+
+ // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
+ normalizeRule: function( data ) {
+ if ( typeof data === "string" ) {
+ var transformed = {};
+ $.each(data.split(/\s/), function() {
+ transformed[this] = true;
+ });
+ data = transformed;
+ }
+ return data;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Validator/addMethod
+ addMethod: function( name, method, message ) {
+ $.validator.methods[name] = method;
+ $.validator.messages[name] = message !== undefined ? message : $.validator.messages[name];
+ if ( method.length < 3 ) {
+ $.validator.addClassRules(name, $.validator.normalizeRule(name));
+ }
+ },
+
+ methods: {
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/required
+ required: function( value, element, param ) {
+ // check if dependency is met
+ if ( !this.depend(param, element) ) {
+ return "dependency-mismatch";
+ }
+ if ( element.nodeName.toLowerCase() === "select" ) {
+ // could be an array for select-multiple or a string, both are fine this way
+ var val = $(element).val();
+ return val && val.length > 0;
+ }
+ if ( this.checkable(element) ) {
+ return this.getLength(value, element) > 0;
+ }
+ return $.trim(value).length > 0;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/email
+ email: function( value, element ) {
+ // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
+ return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/url
+ url: function( value, element ) {
+ // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
+ return this.optional(element) || /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/date
+ date: function( value, element ) {
+ return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
+ dateISO: function( value, element ) {
+ return this.optional(element) || /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/.test(value);
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/number
+ number: function( value, element ) {
+ return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/digits
+ digits: function( value, element ) {
+ return this.optional(element) || /^\d+$/.test(value);
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
+ // based on http://en.wikipedia.org/wiki/Luhn
+ creditcard: function( value, element ) {
+ if ( this.optional(element) ) {
+ return "dependency-mismatch";
+ }
+ // accept only spaces, digits and dashes
+ if ( /[^0-9 \-]+/.test(value) ) {
+ return false;
+ }
+ var nCheck = 0,
+ nDigit = 0,
+ bEven = false;
+
+ value = value.replace(/\D/g, "");
+
+ for (var n = value.length - 1; n >= 0; n--) {
+ var cDigit = value.charAt(n);
+ nDigit = parseInt(cDigit, 10);
+ if ( bEven ) {
+ if ( (nDigit *= 2) > 9 ) {
+ nDigit -= 9;
+ }
+ }
+ nCheck += nDigit;
+ bEven = !bEven;
+ }
+
+ return (nCheck % 10) === 0;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/minlength
+ minlength: function( value, element, param ) {
+ var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
+ return this.optional(element) || length >= param;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
+ maxlength: function( value, element, param ) {
+ var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
+ return this.optional(element) || length <= param;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
+ rangelength: function( value, element, param ) {
+ var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
+ return this.optional(element) || ( length >= param[0] && length <= param[1] );
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/min
+ min: function( value, element, param ) {
+ return this.optional(element) || value >= param;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/max
+ max: function( value, element, param ) {
+ return this.optional(element) || value <= param;
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/range
+ range: function( value, element, param ) {
+ return this.optional(element) || ( value >= param[0] && value <= param[1] );
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
+ equalTo: function( value, element, param ) {
+ // bind to the blur event of the target in order to revalidate whenever the target field is updated
+ // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
+ var target = $(param);
+ if ( this.settings.onfocusout ) {
+ target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
+ $(element).valid();
+ });
+ }
+ return value === target.val();
+ },
+
+ // http://docs.jquery.com/Plugins/Validation/Methods/remote
+ remote: function( value, element, param ) {
+ if ( this.optional(element) ) {
+ return "dependency-mismatch";
+ }
+
+ var previous = this.previousValue(element);
+ if (!this.settings.messages[element.name] ) {
+ this.settings.messages[element.name] = {};
+ }
+ previous.originalMessage = this.settings.messages[element.name].remote;
+ this.settings.messages[element.name].remote = previous.message;
+
+ param = typeof param === "string" && {url:param} || param;
+
+ if ( previous.old === value ) {
+ return previous.valid;
+ }
+
+ previous.old = value;
+ var validator = this;
+ this.startRequest(element);
+ var data = {};
+ data[element.name] = value;
+ $.ajax($.extend(true, {
+ url: param,
+ mode: "abort",
+ port: "validate" + element.name,
+ dataType: "json",
+ data: data,
+ success: function( response ) {
+ validator.settings.messages[element.name].remote = previous.originalMessage;
+ var valid = response === true || response === "true";
+ if ( valid ) {
+ var submitted = validator.formSubmitted;
+ validator.prepareElement(element);
+ validator.formSubmitted = submitted;
+ validator.successList.push(element);
+ delete validator.invalid[element.name];
+ validator.showErrors();
+ } else {
+ var errors = {};
+ var message = response || validator.defaultMessage( element, "remote" );
+ errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
+ validator.invalid[element.name] = true;
+ validator.showErrors(errors);
+ }
+ previous.valid = valid;
+ validator.stopRequest(element, valid);
+ }
+ }, param));
+ return "pending";
+ }
+
+ }
+
+});
+
+// deprecated, use $.validator.format instead
+$.format = $.validator.format;
+
+}(jQuery));
+
+// ajax mode: abort
+// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
+// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
+(function($) {
+ var pendingRequests = {};
+ // Use a prefilter if available (1.5+)
+ if ( $.ajaxPrefilter ) {
+ $.ajaxPrefilter(function( settings, _, xhr ) {
+ var port = settings.port;
+ if ( settings.mode === "abort" ) {
+ if ( pendingRequests[port] ) {
+ pendingRequests[port].abort();
+ }
+ pendingRequests[port] = xhr;
+ }
+ });
+ } else {
+ // Proxy ajax
+ var ajax = $.ajax;
+ $.ajax = function( settings ) {
+ var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
+ port = ( "port" in settings ? settings : $.ajaxSettings ).port;
+ if ( mode === "abort" ) {
+ if ( pendingRequests[port] ) {
+ pendingRequests[port].abort();
+ }
+ pendingRequests[port] = ajax.apply(this, arguments);
+ return pendingRequests[port];
+ }
+ return ajax.apply(this, arguments);
+ };
+ }
+}(jQuery));
+
+// provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
+// handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
+(function($) {
+ $.extend($.fn, {
+ validateDelegate: function( delegate, type, handler ) {
+ return this.bind(type, function( event ) {
+ var target = $(event.target);
+ if ( target.is(delegate) ) {
+ return handler.apply(target, arguments);
+ }
+ });
+ }
+ });
+}(jQuery));
diff --git a/lib/assets/javascripts/surveyor/surveyor_validations.js b/lib/assets/javascripts/surveyor/surveyor_validations.js
new file mode 100644
index 00000000..6b8d4c5b
--- /dev/null
+++ b/lib/assets/javascripts/surveyor/surveyor_validations.js
@@ -0,0 +1,18 @@
+$(document).ready(function() {
+
+ // surveyor class rule validations go here
+ // class_names should be prefixed with 'surveyor_' to avoid
+ // class naming conflicts
+ // e.g. jQuery.validator.addClassRules("surveyor_minlength_10", {min: 10} );
+ jQuery.validator.addClassRules("surveyor_required", {required: true} );
+ jQuery.validator.addClassRules("surveyor_float_answer", {number: true} );
+ jQuery.validator.addClassRules("surveyor_integer_answer", {digits: true} );
+ jQuery.validator.addClassRules("surveyor_time_answer", {time: true} );
+
+ // see http://jqueryvalidation.org/documentation/ for the list of
+ // built-in validations
+
+ // when class rules are setup then let's call validate()
+ $("#survey_form").validate();
+
+});
\ No newline at end of file
diff --git a/lib/assets/stylesheets/surveyor/validations.css b/lib/assets/stylesheets/surveyor/validations.css
new file mode 100644
index 00000000..7599d002
--- /dev/null
+++ b/lib/assets/stylesheets/surveyor/validations.css
@@ -0,0 +1,6 @@
+label.error {
+ color: crimson;
+ background-color: pink;
+ border: 1px solid crimson;
+ padding: .125em .5em .25em .5em
+}
\ No newline at end of file
diff --git a/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js b/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js
index c24c4964..4b132c92 100644
--- a/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js
+++ b/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js
@@ -3,4 +3,7 @@
//= require surveyor/jquery-ui-timepicker-addon
//= require surveyor/jquery.selectToUISlider
//= require surveyor/jquery.surveyor
-//= require surveyor/jquery.maskedinput
\ No newline at end of file
+//= require surveyor/jquery.maskedinput
+//= require surveyor/jquery.validate
+//= require surveyor/jquery.validate.additional-methods
+//= require surveyor/surveyor_validations
\ No newline at end of file
diff --git a/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css b/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css
index f9a9df9b..b76b4e3a 100644
--- a/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css
+++ b/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css
@@ -4,6 +4,7 @@
*= require surveyor/jquery-ui-timepicker-addon
*= require surveyor/ui.slider.extras
*= require surveyor/results
+*= require surveyor/validations
*= require surveyor
*= require custom
*/
\ No newline at end of file
diff --git a/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb b/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb
index 65352fdb..5e7b811d 100644
--- a/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb
+++ b/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb
@@ -7,6 +7,10 @@
# A label is a question that accepts no answers
label "These questions are examples of the basic supported input types"
+ # Example of a mandatory question
+ question "Please enter something. This is required.", :is_mandatory => true
+ answer :string
+
# A basic question with radio buttons
question "What is your favorite color?", :pick => :one
answer "red"
@@ -179,7 +183,7 @@
# Questions may also have input masks with fancy placeholders
q "What is your phone number?"
a :string, :input_mask => '(999)999-9999', :input_mask_placeholder => '#'
-
+
end
section "Complicated questions" do
diff --git a/lib/surveyor/models/validation_methods.rb b/lib/surveyor/models/validation_methods.rb
index 3a86ac37..701d69ce 100644
--- a/lib/surveyor/models/validation_methods.rb
+++ b/lib/surveyor/models/validation_methods.rb
@@ -7,7 +7,7 @@ def self.included(base)
base.send :has_many, :validation_conditions, :dependent => :destroy
# Scopes
-
+
@@validations_already_included ||= nil
unless @@validations_already_included
# Validations
@@ -15,10 +15,10 @@ def self.included(base)
base.send :validates_format_of, :rule, :with => /^(?:and|or|\)|\(|[A-Z]|\s)+$/
# this causes issues with building and saving
# base.send :validates_numericality_of, :answer_id
-
+
@@validations_already_included = true
end
-
+
# Whitelisting attributes
base.send :attr_accessible, :answer, :answer_id, :rule, :message
end