Created
April 15, 2014 17:25
-
-
Save MiguelDebruyne/10749943 to your computer and use it in GitHub Desktop.
JavaScript: Basic validator form validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formValidation() { | |
jQuery.validator.addMethod("twowords", function(value, element) { | |
var tagCheckRE = new RegExp("(\\w+)(\\s+)(\\w+)"); | |
return tagCheckRE.test(value); | |
}, "At least two words."); | |
var numInputFieldsToCheck = | |
$('#contactForm').find('input').length -1 + | |
$('#contactForm').find('textarea').length; | |
var indexOfErrorShowing = 0; | |
var showErrors = false; | |
var validatorContact = $('#contactForm').validate({ | |
onfocusout: function(element, event){ | |
$(element).valid(); | |
}, | |
showErrors: function (errorMap, errorList) { | |
indexOfErrorShowing++; | |
if (!Modernizr.input.placeholder && indexOfErrorShowing > numInputFieldsToCheck) { | |
showErrors = true; | |
} else if (Modernizr.input.placeholder) { | |
showErrors = true; | |
} | |
if (showErrors) { | |
$("#errorCon").html("<p>Wypełnij prawidłowo wymagane pola</p>"); | |
if (this.numberOfInvalids() > 0) { | |
$("#errorCon").css('visibility', 'visible'); | |
} else { | |
$("#errorCon").css('visibility', 'hidden'); | |
} | |
this.defaultShowErrors(); | |
} | |
}, | |
errorPlacement: function () { | |
return false; | |
}, | |
rules: { | |
name: { | |
required: true, | |
twowords: true | |
}, | |
email: { | |
required: true, | |
email: true | |
}, | |
telephonenumber: { | |
digits: true, | |
minlength: 7 | |
}, | |
subject: { | |
required: true | |
}, | |
message: { | |
required: true | |
} | |
}, | |
messages: { | |
name: { | |
required: 'Please fill in your name', | |
twowords: 'Please fill in your full name' | |
}, | |
email: { | |
required: 'Please fill in your email', | |
email: 'It is not a valid email' | |
}, | |
telephonenumber: { | |
digits: 'Please only fill in numbers', | |
minlength: 'Please fill in your full phone number' | |
}, | |
subject: { | |
required: 'Please fill in a subject' | |
}, | |
message: { | |
required: 'Please fill in your message' | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment