вынес в отдельный файл все функции и обработчики
$(document).ready(function(){
$('input[type=checkbox]').val('Нет');
$('input[type=checkbox]:checked').val('Да');
$('input[type=checkbox]').change(function () {
if (this.checked) {
this.value = 'Да';
}
else {
this.value = 'Нет';
}
});
});
$(document).ready(function(){
$('input[type=radio]').val('Не выбрано');
$('input[type=radio]').click(function () {
name = $(this).attr("name")
label= $(this).next("label");
radio = document.getElementsByName(name);
$.each(radio, function( key, value ) {
value.value = label.text()
});
});
});
////////////////////////////////////////////////////////
////////////Функции проверки формы начало///////////////
////////////////////////////////////////////////////////
function validnot(text) {return true}
///////////Проверка заполнения телефона/////////
function validphone(text) {
text_val = text.val();
var pattern1 = /^\+[\d]{1}\ \([\d]{2,3}\)\ [\d]{2,3}-[\d]{2,3}-[\d]{2,3}$/;
var pattern2 = /^\d[\d\(\)\ -]{4,14}\d$/;
var pattern3 = /^[\d]{10,11}$/;
if (pattern1.test(text_val) == true) {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else if (pattern2.test(text_val) == true) {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else if (pattern3.test(text_val) == true) {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else {
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
}
///////////Проверка заполнения radio/////////
function validradio(text) {
countradio = 0;
$.each( text, function( key, value ) {
if ($(value).is(':checked')) {
countradio++;
}
else {}
});
if (countradio > 0){
$.each( text, function( key, value ) {
label = $(value).next("label");
$(label).next(".error_block").css("display","none");
});
}
else {
$.each( text, function( key, value ) {
label = $(value).next("label");
$(label).next(".error_block").css("display","block");
});
errorcount++;
}
}
///////////Проверка Select/////////
function validselect(text) {
text_val = text.val();
if (text_val == "" || text_val == " "){
errorcount++;
}
else {}
}
///////////Проверка checkbox/////////
function validcheck(text){
text_val = text.val();
label = $(text).next("label");
if (text.is(':checked')){
$(label).next(".error_block").css("display","none");
}
else {
$(label).next(".error_block").css("display","block");
errorcount++;
}
}
///////////////////// Проверка длинны текста////////////////////
function validlenght(text) {
text_val = text.val();
var limit = 5; //////Максимальная длинна текста/////////
var text_length = text_val.length;
if (text_val == "" || text_val == " "){
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
else if (text_length > limit){
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
else {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
}
///////////Проверка наличия текста/////////
function validtext(text) {
text_val = text.val();
if (text_val == "" || text_val == " "){
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
else {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
}
///////////Проверка наличия текста (только буквы)/////////
function validname(text) {
text_val = text.val();
var pattern = /^[a-zа-яё]+$/i;
if (pattern.test(text_val) == true) {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else {
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
}
///////////Проверка заполнения E-mail/////////
function validmail(text) {
text_val = text.val();
var pattern = /^[\w\.\d-_]+@[\w\.\d-_]+\.\w{2,4}$/i;
if (pattern.test(text_val) == true) {
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else {
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
}
/////////Проверка заполнения даты/////////
function validdate(text) {
text_val = text.val();
var pattern1 = /^[\d]{1,2}\/[\d]{1,2}\/[\d]{4}$/;
var pattern2 = /^[\d]{1,2}\.[\d]{1,2}\.[\d]{4}$/;
var pattern3 = /^[\d]{1,2}\,[\d]{1,2}\,[\d]{4}$/;
var pattern4 = /^[\d]{1,2}\-[\d]{1,2}\-[\d]{4}$/;
var pattern5 = /^[\d]{1,2}\\[\d]{1,2}\\[\d]{4}$/;
if (pattern1.test(text_val) == true){
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else if (pattern2.test(text_val) == true){
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else if (pattern3.test(text_val) == true){
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else if (pattern4.test(text_val) == true){
$(text).removeClass("error");
$(text).addClass("accept");
$(text).next(".error_block").css("display","none");
}
else {
$(text).removeClass("accept");
$(text).addClass("error");
$(text).next(".error_block").css("display","block");
errorcount++;
}
}
/////////////////////////////////////////////////////////
//////////////Функции проверки формы конец///////////////
/////////////////////////////////////////////////////////
function set_alerts_forms(text){
$.each(text, function () {
$.each(this, function (name, value) {
var name = document.getElementsByName(name);
alert_block = $("<p class='error_block'>"+value+"</p>");
checklabel(name);
function checklabel(text){
if($(text).next().is('label')) {
label = $(text).next("label");
$(label).after(alert_block)
}
else {
$(text).after(alert_block)
}
};
});
});
}
function set_valids_forms(text){
$.each(text, function () {
$.each(this, function (name, value) {
var name = document.getElementsByName(name);
$(name).change(function(){eval(value)($(name))});
});
});
}
function set_valids_onsend_forms(text){
$.each(text, function () {
$.each(this, function (name, value) {
var name = document.getElementsByName(name);
eval(value)($(name));
});
});
}
function set_messege_forms(text){
$.each(text, function () {
$.each(this, function (name, value) {
var name = document.getElementsByName(name);
text_val = $(name).val();
key = value;
message += key +' '+ text_val +'\r\n';
});
});
}
function sendform(key, validator, form_block){
errorcount = 0;
set_valids_onsend_forms(validator);
if (errorcount == 0){
message = '';
set_messege_forms(key);
alert('Ваше сообщение успешно отправлено');
alert(message);
clearform(form_block);
}
else {
return false;
}
}
function clearform (text){
$(text).each(function(){
elem = $(this).find('input[type=text],textarea');
$.each(elem, function (name, value){
value.value = '';
});
});
}
function MyMailer(nameform, validators, alerts, keys){
var this_form = document.getElementById(nameform);
var Submit = $(this_form).find("button");
errorcount = 0;
set_valids_forms(validators);
set_alerts_forms(alerts);
$(Submit).click(function(){
sendform(keys, validators, alerts)
})
}