JQuery表单验证序列
我正在编写一个JQuery表单验证脚本(第一个项目-别笑),到目前为止,我有以下代码似乎可以工作 我希望此验证代码按顺序执行,自上而下检查表单值。现在所有的东西都在同时开火。我怎样才能做到这一点 谢谢大家!JQuery表单验证序列,jquery,validation,Jquery,Validation,我正在编写一个JQuery表单验证脚本(第一个项目-别笑),到目前为止,我有以下代码似乎可以工作 我希望此验证代码按顺序执行,自上而下检查表单值。现在所有的东西都在同时开火。我怎样才能做到这一点 谢谢大家! // ----------------------------------------------- // FORM VALIDATION // ----------------------------------------------- function mcValidateFo
// -----------------------------------------------
// FORM VALIDATION
// -----------------------------------------------
function mcValidateForm() {
// -----------------------------------------------
// CHECK - EMPTY INPUT TEXT
// -----------------------------------------------
$('.mcRequired').each(function() {
var mcEmptyCheck = $.trim($(this).val());
if(mcEmptyCheck == '' || mcEmptyCheck.length < 3) {
mcResponse('- Please fill in the required field!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - VALID EMAIL FORMAT
// -----------------------------------------------
$('.mcEmail').each(function() {
var mcEmailCheck = $(this).val();
var mcEmailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!mcEmailCheck.match(mcEmailRegex)) {
mcResponse('- Incorrect Email format!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
});
// -----------------------------------------------
// CHECK - VALID WEB ADDRESS - URL
// -----------------------------------------------
$('.mcWebsite').each(function() {
var mcUrlCheck = $(this).val();
var mcUrlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
if(!mcUrlCheck.match(mcUrlRegex)) {
mcResponse('- Incorrect Website Address format!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - SINGLE SELECT SELECTION
// -----------------------------------------------
$('.mcMenu').each(function() {
var mcMenuCheck = $(this).val();
if(mcMenuCheck == null || mcMenuCheck == 'Please Select One') {
mcResponse('- Please make a Selection!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else if(mcMenuCheck != null) {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - MULTI SELECT SELECTION
// -----------------------------------------------
$('.mcList').each(function() {
var mcSelectCheck = $(this).val();
if(mcSelectCheck == null) {
mcResponse('- Please make a Selection!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else if(mcSelectCheck != null) {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK SINGLE CHECKBOX
// -----------------------------------------------
$('.mcCbxSingle').each(function() {
var mcCbxCheck = $(this);
if(!mcCbxCheck.is(':checked')) {
mcResponse('- Please check the checkbox!', true);
$(this).parents(':eq(1)').addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else{
$(this).parents(':eq(1)').removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK CHECKBOX GROUPS
// -----------------------------------------------
$('.mcCbxGroup').each(function() {
if($(this).find('input[type=checkbox]:checked').length == 0) {
mcResponse('- Please check at least one checkbox in the group!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK RADIO GROUP
// -----------------------------------------------
$('.mcRadGroup').each(function() {
if($(this).find('input[type=radio]:checked').length == 0) {
mcResponse('- Please select a radio button!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// FILE UPLOAD - SINGLE
// -----------------------------------------------
$('.mcFileUpSingle').each(function() {
if($(this).find('input[type=file]').val() == '') {
mcResponse('- Please select a file to upload!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// FILE UPLOAD - GROUP
// -----------------------------------------------
$('.mcFileUpGroup').each(function() {
$(this).addClass('mcError').fadeOut().fadeIn();
$('.mcFileUpGroup input[type=file]').each(function() {
if($(this).val() == '') {
mcResponse('- Upload file not selected!', true);
$(this).parent().addClass('mcError');
$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
return false;
}
else{
$(this).removeClass('mcError');
$(this).parent().removeClass('mcError');
}
});
});
// -----------------------------------------------
// CHECK RECAPTCHA
// -----------------------------------------------
var mcRecaptchaDiv = $('#recaptcha_area');
var mcReCaptcha = $('input[id=recaptcha_response_field]');
var mcReCaptchaVal = mcReCaptcha.val();
if(mcReCaptcha.is(':visible')) {
if($.trim(mcReCaptchaVal) == ''){
mcResponse('- Please enter the Captcha text as presented below!', true);
$(mcRecaptchaDiv).addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop: $(mcRecaptchaDiv).offset().top},'slow');
$(mcReCaptcha).focus();
return false;
} else {
$(mcRecaptchaDiv).removeClass('mcError');
}
}
}
//-----------------------------------------------
//表单验证
// -----------------------------------------------
函数mcValidateForm(){
// -----------------------------------------------
//选中-空输入文本
// -----------------------------------------------
$('.mcRequired')。每个(函数(){
var mcEmptyCheck=$.trim($(this.val());
如果(mcEmptyCheck=''| | mcEmptyCheck.length<3){
mcResponse('-请填写必填字段!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//检查-有效的电子邮件格式
// -----------------------------------------------
$('.mcEmail')。每个(函数(){
var mcEmailCheck=$(this.val();
var mcEmailRegex=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
如果(!mcEmailCheck.match(mcEmailRegex)){
mcResponse('-error Email format!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
});
// -----------------------------------------------
//检查-有效网址-URL
// -----------------------------------------------
$('.mcWebsite')。每个(函数(){
var mcUrlCheck=$(this.val();
var mcUrlRegex=/^(https?:\/\/)?([\da-z\.-]+)\([a-z\.]{2,6})([\/\w\.-]*)*\/?$;
如果(!mcUrlCheck.match(mcUrlRegex)){
mcResponse('-不正确的网址格式!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//选中-单选选择
// -----------------------------------------------
$('.mcMenu')。每个(函数(){
var mcMenuCheck=$(this.val();
如果(mcMenuCheck==null | | mcMenuCheck=='请选择一个'){
mcResponse('-请选择!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
else if(mcMenuCheck!=null){
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//选中-多选选择
// -----------------------------------------------
$('.mcList')。每个(函数(){
var mcSelectCheck=$(this.val();
if(mcSelectCheck==null){
mcResponse('-请选择!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
else if(mcSelectCheck!=null){
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//选中单个复选框
// -----------------------------------------------
$('.mcCbxSingle')。每个(函数(){
var mcCbxCheck=$(此项);
如果(!mcCbxCheck.is(':checked')){
mcResponse('-请选中复选框!',true);
$(this).parents(':eq(1)').addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this).parents(':eq(1)').removeClass('mcError');
}
});
// -----------------------------------------------
//复选框组
// -----------------------------------------------
$('.mcCbxGroup')。每个(函数(){
if($(this).find('input[type=checkbox]:checked')。length==0{
mcResponse('-请在组中至少选中一个复选框!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//检查无线电组
// -----------------------------------------------
$('.mcRadGroup')。每个(函数(){
if($(this).find('input[type=radio]:checked')。length==0{
mcResponse('-请选择一个单选按钮!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this.removeClass('mcError');
}
});
// -----------------------------------------------
//文件上载-单个
// -----------------------------------------------
$('.mcFileUpSingle')。每个(函数(){
if($(this).find('input[type=file]').val()=''){
mcResponse('-请选择要上载的文件!',true);
$(this.addClass('mcError').fadeOut().fadeIn();
$('html,body').stop().animate({scrollTop:$(this.offset().top},'slow');
返回false;
}
否则{
$(this.removeClass('mcError');
}
});
//
// -----------------------------------------------
// FORM VALIDATION
// -----------------------------------------------
function mcValidateForm() {
// -----------------------------------------------
// CHECK - EMPTY INPUT TEXT
// -----------------------------------------------
$('.mcRequired').each(function() {
var mcEmptyCheck = $.trim($(this).val());
if(mcEmptyCheck == '' || mcEmptyCheck.length < 3) {
mcResponse('- Please fill in the required field!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - VALID EMAIL FORMAT
// -----------------------------------------------
$('.mcEmail').each(function() {
var mcEmailCheck = $(this).val();
var mcEmailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!mcEmailCheck.match(mcEmailRegex)) {
mcResponse('- Incorrect Email format!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
});
// -----------------------------------------------
// CHECK - VALID WEB ADDRESS - URL
// -----------------------------------------------
$('.mcWebsite').each(function() {
var mcUrlCheck = $(this).val();
var mcUrlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
if(!mcUrlCheck.match(mcUrlRegex)) {
mcResponse('- Incorrect Website Address format!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - SINGLE SELECT SELECTION
// -----------------------------------------------
$('.mcMenu').each(function() {
var mcMenuCheck = $(this).val();
if(mcMenuCheck == null || mcMenuCheck == 'Please Select One') {
mcResponse('- Please make a Selection!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
///return false;
}
else if(mcMenuCheck != null) {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK - MULTI SELECT SELECTION
// -----------------------------------------------
$('.mcList').each(function() {
var mcSelectCheck = $(this).val();
if(mcSelectCheck == null) {
mcResponse('- Please make a Selection!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else if(mcSelectCheck != null) {
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK SINGLE CHECKBOX
// -----------------------------------------------
$('.mcCbxSingle').each(function() {
var mcCbxCheck = $(this);
if(!mcCbxCheck.is(':checked')) {
mcResponse('- Please check the checkbox!', true);
$(this).parents(':eq(1)').addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else{
$(this).parents(':eq(1)').removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK CHECKBOX GROUPS
// -----------------------------------------------
$('.mcCbxGroup').each(function() {
if($(this).find('input[type=checkbox]:checked').length == 0) {
mcResponse('- Please check at least one checkbox in the group!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// CHECK RADIO GROUP
// -----------------------------------------------
$('.mcRadGroup').each(function() {
if($(this).find('input[type=radio]:checked').length == 0) {
mcResponse('- Please select a radio button!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// FILE UPLOAD - SINGLE
// -----------------------------------------------
$('.mcFileUpSingle').each(function() {
if($(this).find('input[type=file]').val() == '') {
mcResponse('- Please select a file to upload!', true);
$(this).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else{
$(this).removeClass('mcError');
}
});
// -----------------------------------------------
// FILE UPLOAD - GROUP
// -----------------------------------------------
$('.mcFileUpGroup').each(function() {
$(this).addClass('mcError').fadeOut().fadeIn();
$('.mcFileUpGroup input[type=file]').each(function() {
if($(this).val() == '') {
mcResponse('- Upload file not selected!', true);
$(this).parent().addClass('mcError');
//$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
//return false;
}
else{
$(this).removeClass('mcError');
$(this).parent().removeClass('mcError');
}
});
});
// -----------------------------------------------
// CHECK RECAPTCHA
// -----------------------------------------------
var mcRecaptchaDiv = $('#recaptcha_area');
var mcReCaptcha = $('input[id=recaptcha_response_field]');
var mcReCaptchaVal = mcReCaptcha.val();
if(mcReCaptcha.is(':visible')) {
if($.trim(mcReCaptchaVal) == ''){
mcResponse('- Please enter the Captcha text as presented below!', true);
$(mcRecaptchaDiv).addClass('mcError').fadeOut().fadeIn();
//$('html,body').stop().animate({scrollTop: $(mcRecaptchaDiv).offset().top},'slow');
//$(mcReCaptcha).focus();
//return false;
} else {
$(mcRecaptchaDiv).removeClass('mcError');
}
}
var $errors = $("mcError");
if($errors.size() > 0){
$('html,body').stop().animate({scrollTop: $errors.filter(":last").offset().top},'slow');
return false;
}
}