我一直试图问这个问题,但是我的代码很长等等,我会再试一次。
不幸的是,我无法总结代码,但我只想知道我有大约 4 个返回 true 或 false 的验证函数,例如:
//Basic validation
function validate_Email(email_values){
var email_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(email_reg.test(email_values) == false) {
console.log('invalid email');
return false;
} else {
console.log('valid email');
return true;
};
};
function validate_Cell(cell_values){
if (cell_values.toString().length != 10) {
return false;
console.log('invalid cell');
}else if (cell_values.toString().length = 10) {
return true;
console.log('valid cell');
};
};
function validate_Pass(pass_values){
if (pass_values.toString().length != 13) {
console.log($(pass_values).toString().length);
console.log(pass_values);
return false;
console.log('invalid passport');
}else if (pass_values.toString().length = 13) {
console.log(pass_values);
return true;
console.log('valid passport');
};
};
function validate_Code(code_values){
if (code_values.toString().length != 4) {
console.log($(code_values).toString().length);
console.log(code_values);
return false;
console.log('invalid passport');
}else if (code_values.toString().length = 4) {
console.log(code_values);
return true;
console.log('valid codes');
};
};
//Basic Validation
以下是如何验证电子邮件的示例:
$(document).on('blur', 'input.email', function() {
var $this = $(this);
var input_groups = $this.parent();
$.each(input_groups , function(i){
var inpg = input_groups[i];
email_values = $.map($(inpg).children('input'), function(e,i){
return $(e).val();
}).join('');
validate_Email(email_values, input_groups.parent().parent().parent());
});
});
我还有一些其他更复杂的验证,它们返回真或假。我只想知道如何不发送,如果有的话。
以下是我获取和发送数据的方式:
//Get Field Values
var result = {};
var dependants;
var mainmember;
var dep_counter = 0;
function getValues(){
jQuery('div[class*="mainmember"]').each(function(k, v){
mainmember = {};
mainmember['name'] = $(v).find('.main_name').val();
mainmember['surname'] = $(v).find('.main_surname').val();
mainmember['id'] = '';
$(v).find('.id').each(function(){
mainmember['id'] += $(this).val();
});
mainmember['age'] = '';
$(v).find('.age').each(function(){
mainmember['age'] += $(this).val();
});
mainmember['gender'] = $(v).find('.gender').val();
mainmember['townofbirth'] = $(v).find('.main_town').val();
mainmember['email'] = $(v).find('.email').val();
mainmember['contact'] = '';
$(v).find('.cell').each(function(){
mainmember['contact'] += $(this).val();
});
mainmember['passport'] = '';
$(v).find('.pass').each(function(){
mainmember['passport'] += $(this).val();
});
mainmember['postal'] = $(v).find('.main_postaladdress').val();
mainmember['residential_code'] = '';
$(v).find('.res_code').each(function(){
mainmember['residential_code'] += $(this).val();
});
mainmember['postal_code'] = '';
$(v).find('.post_code').each(function(){
mainmember['postal_code'] += $(this).val();
});
mainmember['residential'] = $(v).find('.main_residential').val();
result['mainmember'] = mainmember;
});
result['dependants'] = [];
jQuery('div[class*="dependant"]').each(function(k, v){
dep_counter++
dependants = {};
dependants['name'] = $(v).find('.name').val();
dependants['surname'] = $(v).find('.surname').val();
dependants['id'] = '';
$(v).find('.id').each(function(){
dependants['id'] += $(this).val();
});
dependants['age'] = '';
$(v).find('.age').each(function(){
dependants['age'] += $(this).val();
});
dependants['gender'] = $(v).find('.gender').val();
dependants['townofbirth'] = $(v).find('.town').val();
dependants['cell'] = '';
$(v).find('.cell').each(function(){
dependants['cell'] += $(this).val();
});
dependants['email'] = $(v).find('.email').val();
dependants['passport'] = '';
$(v).find('.pass').each(function(){
dependants['passport'] += $(this).val();
});
dependants['relationship'] = $(v).find('.relationship:checked').val();
result['dependants'].push(dependants);
});
};
//Get Field Values
//submit function
jQuery('#submit').click(function(){
getValues();
var jsonData = JSON.stringify(result);
jQuery.ajax({
type: "POST",
url: "mail.php",
dataType: "json",
beforeSend: function() {
},
data: {parameters: jsonData}
});
console.log('Sending error');
console.log(jsonData);
});
//submit function
这是完整的代码(警告它有很多代码):http: //jsfiddle.net/dawidvdh/tphjh/
非常感谢任何帮助。:)